The PHP file upload form is an excerpt from drupal's code

  • 2020-03-31 21:29:34
  • OfStack

Example of a drupal file upload form
 
function upload_form() { 
$form = array(); 
// If this #attribute is not present, upload will fail on submit 
$form['#attributes']['enctype'] = 'multipart/form-data'; 
$form['file_upload'] = array( 
'#title' => t('Upload file'), 
'#type' => 'file', 
); 
$form['submit_upload'] = array( 
'#type' => 'submit', 
'#value' => 'Submit' 
); 
return $form; 
} 
function upload_submit($form, &$form_state) { 
$validators = array(); 
$dest = file_directory_path(); 
$file = file_save_upload('file_upload', $validators, $dest); 
//$file will be 0 if the upload doesn't exist, or the $dest directory 
//isn't writable 
if ($file != 0) { 
$file->filepath; //File relative path
} 
else { 
form_set_error('myform', t("Failed to save the file.")); 
} 
} 

PHP file upload function code example tutorial
In the development of PHP website, how to realize the file upload function of PHP program has been a novice topic. And file upload function is generally used, such as picture upload. Today, combined with specific code examples and detailed annotations and we share how to write PHP file upload code, suitable for PHP beginners to learn.
PHP code example is mainly about the picture upload, after reading the program you can modify the relevant file type can achieve the upload function of other files.
Programming environment
PHP5.2.4, basically PHP4.3 above, this code can be used
The preparatory work
Check the item upload_tmp_dir
If the PHP development environment is built on your own, you need to edit the php.ini file before writing the file upload program, find and edit the upload_tmp_dir option, which sets the temporary folder for uploading files to the server, such as upload_tmp_dir = E:/phpos/uploads, and then restart Apache. Upload_tmp_dir is usually set if your PHP development environment USES a simple-and-click installation package, or you can check the configuration with the phpinfo() function.
Write an upload file that sets up the file upload form
 
<form enctype="multipart/form-data" action="upload.php" method="post"> 
<input type="hidden" name="max_file_size" value="100000"> 
<input name="userfile" type="file">   
<input type="submit" value=" Upload a file "> 
</form> 

Pay attention to
1. Enctype = "multipart/form-data" in the form must be specified to let the server know that the file contains regular form information.
Upload files can 2, there must be a set maximum length form area, allowing a maximum of uploading files (in bytes), it is the hidden domain, namely max_file_size, by setting the Value (Value) can limit the upload file size, avoid the user take the time to wait after uploading large files to find the file is too big trouble. However, it is generally possible for others to bypass this value, so to be safe, it is best to configure upload_max_filesize in the php.ini file and set the file upload size to 2M by default.
File uploader
 
function uploadfile($type,$name,$ext,$size,$error,$tmp_name,$targetname,$upload_dir) 
{ 
$MAX_SIZE = 2000000; 
$FILE_MIMES = array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png'); 
$FILE_EXTS = array('.jpg','.gif','.png','.JPG','.GIF','.PNG'); 
$file_path = $upload_dir.$targetname; 
if(!is_dir($upload_dir)) 
{ 
if(!mkdir($upload_dir)) 
die(" The file upload directory does not exist and cannot be created "); 
if(!chmod($upload_dir,0755)) 
die(" The permissions of the file upload directory cannot be set to read and write "); 
} 
if($size>$MAX_SIZE) 
die(" The size of the uploaded file exceeds the specified size "); 
if($size == 0) 
die(" Please select the file to upload "); 
if(!in_array($type,$FILE_MIMES) || !in_array($ext,$FILE_EXTS)) 
die(" Please upload the required file type "); 
if(!move_uploaded_file($tmp_name, $file_path)) 
die(" Copy file failed, please upload again "); 
switch($error) 
{ 
case 0: 
return ; 
case 1: 
die(" More files have been uploaded  php.ini  In the  upload_max_filesize  The value of the option limit "); 
case 2: 
die(" The size of the uploaded file is over  HTML  In the form  MAX_FILE_SIZE  Option to specify the value "); 
case 3: 
die(" Only part of the file is uploaded "); 
case 4: 
die(" No files were uploaded "); 
} 
} 

Parameters that
$type,$name,$size,$error, and $tmp_name correspond to the related variables in the global variable $_FILES, that is:
$_FILES['userfile']['type'] : the file's MIME type, which requires the browser to provide support for this information, such as the image type "image/ GIF".
$_FILES['userfile']['name'] : the original name of the client file.
$_FILES['userfile']['size'] : the size of the uploaded file in bytes.
$_FILES['userfile']['tmp_name'] : temporary file name stored on the server after the file has been uploaded.
$_FILES['userfile']['error'] : the error code associated with the file upload, i.e
Value: 0: no error occurred, file uploaded successfully.
Value: 1: the uploaded file exceeds the limit of the upload_max_filesize option in php.ini.
Value: 2: the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
Value: 3: only part of the file is uploaded.
Value: 4: no file is uploaded.
$ext: upload file extension
$targetname: the final file name after the file is uploaded
$upload_dir: relative path to which directory to upload
Note:
Line 3 ~ line 6: set the size of image file upload, as well as the MIME type and extension of the file. Since this code is an image file upload program, all image types are listed in the two arrays, such as PNG, GIF, JEPG, etc.
Lines 17 to 24: if the file is empty, the size is 0; If the extension or type of the image file does not match, it pops up.
Line 26: the move_uploaded_file function moves files in the server temporary directory set to upload_tmp_dir to a file specified by $file_path, overwriting the target file if it already exists
How to upload multiple files? Like uploading three files at once
You only need to
 
<input name="userfile" type="file"> 

to
 
<input name="userfile[]" type="file"> 
<input name="userfile[]" type="file"> 
<input name="userfile[]" type="file"> 

When this function is called,$_FILES['userfile']['name'][0] represents the file information related to the first file, and so on.
conclusion
This function is the simplest core code in PHP file upload, image upload is just one of them, only need to modify or expand the information of $FILE_MIMES and $FILE_EXTS array, you can achieve other types of file upload function. On the periphery of the function, you can write other relevant code according to your own needs to achieve other functions, such as the association with the database.

Related articles: