PHP file upload complete guide

  • 2020-03-31 20:42:55
  • OfStack

1. Form part
To allow users to upload files, add an upload attribute to the declaration of the HTML form:
Enctype = "multipart/form - data '
The method for the form must be POST
The form option MAX_FILE_SIZE hidden field is used to limit the size of the uploaded file and must be placed in bytes before the file form element.
Such as:
 
<form enctype='multipart/form-data' id='aa' name='aaa' method='post' action='xxx.php'> 
<input type='hidden' name='MAX_FILE_SIZE' value='2621114' /> 
<input name='upload_file' type='file' /> 
</form> 

2. Process uploaded files
When uploaded, PHP receives an array of information about the file, which can be found in the super global array $_FILES.
For example, if the file input field in the form is named upload_file, then all information about the file is contained in the array $_FILES['upload_file'].
For example, the customer uploaded an "aaa.jpg" image array with the following values:
Name "p5pp.jpg" file name when uploaded
Type "image/jpeg" file type
Tmp_name "/ TMP/PHPJKSDF "server side temporary file name
Error uploads the return value of the error
Size 2045 file actual size

The error in the above array will return different constant values, as follows:
UPLOAD_ERR_OK no error occurred and the file was uploaded successfully
UPLOAD_ERR_INI_SIZE exceeds the limit of the upload_max_filesize option in php.ini
UPLOAD_ERR_FORM_SIZE uplofiles exceed the value of the MAX_FILE_SIZE option in the HTML form. The form $FILES ['up_file']['size'] can be checked in the program for processing
UPLOAD_ERR_PARTIAL file is partially uploaded
UPLOAD_ERR_NO_FILE user does not provide any file uploads
Specific examples of post-upload processing:
 
if(!move_uploaded_file($_FILES['f']['tmp_name'],"uploads/".$_FILES['f']['name'].".jpg")){ 
echo "error"; 
} 

Function:
Move_uploaded_file moves the uploaded temporary file to the specified directory
Example:
Move_uploaded_file (' temporary file name ',' specify file path ')

Is_uploaded_file determines if the file is uploaded via HTTP Post
Example:
 
if(!is_uploaded_file($_FILES['f']['tmp_name'])){ 
echo ' illegal '; 
} 


3. Related parameters

Parameters in php.ini designed for PHP upload:
Whether file_uploads is allowed to upload files, default ON

Upload_tmp_dir upload_tmp_dir upload_tmp_dir upload_tmp_dir upload_tmp_dir upload_tmp_dir upload_tmp_dir upload_tmp_dir

Upload_max_filesize allows the maximum size of the file to be uploaded, with a default of 2M

Post_max_size controls the maximum amount of data that PHP can accept in a form submission using the POST method. If you want to upload with a PHP file, change this value to more than upload_max_filesize

Max_input_time limits the time to accept data via POST/GET/PUT in seconds.

Memory_limit to prevent running scripts from making heavy use of system memory, PHP allows you to define memory usage limits. Set this parameter to specify the maximum memory capacity that a single script can use, which should be appropriately larger than the post_max_size value

Max_execution_time is used to set the number of seconds in which PHP waits for the script to finish executing before the script is forced to terminate. The secondary option limits the looping script, but it also causes the operation to fail when there is a long legal activity, such as uploading a large file. In this case, you must consider increasing this variable.

4. Consider multiple file uploads
You can easily upload multiple files using the $_FILES array. The $_FILES array retrieves all of the file field contents in the client form to get all of the files uploaded in the same form.

5. Break the upload memory limit

Method one:
Change the memory_limit value in php.ini to something larger, such as 64M
Method 2:
Use the Apache Rewrite method to dynamically modify the value of memory_limit. First, create a. Htaccess file and save it in the current directory of the upload file program. The code is as follows:
Php_value memory_limit 100 m
Php_value post_max_size 30 m
Php_value upload_max_filesize 30 m
Php_value max_execution_time 300
Php_value max_input_tim 300
Php_value display_errors On

Related articles: