File Upload Example
Code
<?PHP
//include class, create new Formitable, set primary key field name
include("Formitable.class.php");
$newForm = new Formitable( mysql_connect("server","user","pass"),"db","table" );
//set custom field labels
$newForm->labelFields( array("file_name","desc"),
array("Select a File <small>(max size ".ini_get('upload_max_filesize').")</small>","File Description") );
//force field types
$newForm->forceTypes( array("file_name","desc"), array("file","text") );
//set success message
$newForm->msg_insertSuccess = "<center><label class=\"font\">File successfully uploaded.</label></center>";
//set file field length (file fields are not styleable)
$newForm->fileInputLength=32;
//set error feedback method
$newForm->feedback = "box";
//handle files on submit
if(isset($_FILES)){
//path to upload to (with trailing slash)
$path = "/path/to/files/";
//array of filetypes to check against
$filetypes = array("exe","vb","com","js","php","php3","htm","html");
//should file be include(ed) or exclude(ed) to be acceptable?
$filemode = "exclude";
foreach( $_FILES as $key=>$value ){
//get built-in error code (since PHP 4.2.0)
if( isset($_FILES[$key]['error']) ){ $phpErr = $_FILES[$key]['error']; }
else{ $phpErr = 0; }
if( $_FILES[$key]['name'] && $phpErr!=4 ){
// get the max file size from post (upload_max_filesize in php.ini)
// http://us3.php.net/manual/en/ini.core.php#ini.upload-max-filesize
// to set the upload size smaller than the value in php.ini
// create an .htaccess file in the script directory with the following directive
// php_value upload_max_filesize 1M
$maxSize = $_POST['MAX_FILE_SIZE'];
//test for possible errors: empty/partial file, file too big, file is not really an upload
if( $_FILES[$key]['size']==0 )
$newForm->errMsg[$key] = "File is empty.";
else if( $phpErr==3 )
$newForm->errMsg[$key] = "File incomplete (possibly too big)";
else if( $phpErr==1 || $phpErr==2 || $_FILES[$key]['size']>$maxSize)
$newForm->errMsg[$key] = "File is too big ($maxSize byte limit)";
else if(!is_uploaded_file($_FILES[$key]['tmp_name']))
$newForm->errMsg[$key] = "Error in upload.";
//everything ok, proceed with upload
else {
//get extension and test if acceptable
$ext=split("\.",$_FILES[$key]['name']); $ext=$ext[sizeof($ext)-1];
if( ($filemode=="exclude" && in_array($ext,$filetypes)) ||
($filemode=="include" && !in_array($ext,$filetypes)) )
$newForm->errMsg[$key] = "Unacceptable Filetype.";
//finally copy file
else if( $phpErr!=6 && $phpErr!=7 && copy($_FILES[$key]['tmp_name'], $path.$_FILES[$key]['name']) ){
//set file path to be stored in db
$_POST[$key] = $path.$_FILES[$key]['name'];
}
else $newForm->errMsg[$key] = "Unable to copy file.";
}
} else $newForm->errMsg[$key] = "No file specified.";
}
}
//output form if not submitted or there were errors on submit
if( !isset($_POST['submit']) ||
(isset($_POST['submit']) && $newForm->submitForm() == -1) ){
$newForm->openForm();
$newForm->printField("file_name");
$newForm->printField("desc");
//change submit title and omit reset button
$newForm->closeForm("Upload File","",false);
}
?>
MySQL Tables
CREATE TABLE `formitable_file` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`file_name` tinytext NOT NULL,
`desc` tinytext NOT NULL,
PRIMARY KEY (`id`)
);