Home >Backend Development >PHP Tutorial >PHP implements file upload and download
Principle of file upload:
Upload the client's files to the server, and then move the server's temporary files to the specified directory.
Client configuration:
1. Form page
2. The form is sent by post
3. Add enctype=”multipart/form-data”
$_FILES holds the information of uploaded files
name: The name of the uploaded file
type: MIME type of uploaded file
tmp_name: Temporary file name uploaded to the server
size: upload file size
error: error number of uploaded file
move_uploaded_file(
Such as:
move_uploaded_file(
Also:
copy(
File upload configuration: php.ini
Server side configuration:
file_uploads = on, supports HTTP upload
upload_tmp_dir= , the directory where temporary files are saved
upload_max_filesize = 2M, the maximum size of files allowed to be uploaded
max_file_uploads = 20, the maximum number of files allowed to be uploaded at one time
post_max_size = 8M, the maximum value of data sent by POST method
max_execution_time = -1, sets the maximum execution time allowed before the script is terminated by the parser, in seconds, to prevent poorly written programs from occupying server resources
max_input_time = 60, the maximum time allowed for the script to parse input data, in seconds
max_input_nesting_level = 64, set the nesting depth of input variables
max_input_vars = 1000, how many input variables are accepted (limits apply to
memory_limit = 128M, the maximum independent memory usage of a single thread. That is, a web request gives the definition of the maximum memory usage of the thread.
NOTE: For code that is restricted on the client side, it is possible to render the restriction useless by modifying the code in the browser. So the restrictions should all be on the server side.
Server side limitations:
Limit upload file size (via size)
Limit uploaded file types (in_array(suffix, array), determine whether the file suffix is in the array of the specified type)
Check whether it is a real image type (getimagesize)
Detect whether it is uploaded by HTTP POST method (is_uploaded_file, returning true means it is uploaded through PHP post method)
Generate a unique string:
md5(uniqid(microtime(true),true))
The above introduces the implementation of file upload and download in PHP, including file upload and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.