Home >Backend Development >PHP Tutorial >PHP implements file upload and download

PHP implements file upload and download

WBOY
WBOYOriginal
2016-07-29 08:49:431303browse

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(tmpn ame, destination): Move the temporary file on the server to the specified directory and name it.
Such as:
move_uploaded_file(tmpnam e,"up loads/".filename);
Also:
copy(src,dst);

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 GET, respectively) _POST,$_COOKIE super global The use of the variable) directive mitigates the possibility of a denial of service attack via hash collision. If there are more variables than the number specified by the instruction, E_WARING will be generated, and more input variables will be truncated from the request.
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.
PHP implements file upload and download
PHP implements file upload and download
PHP implements file upload and download
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))

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn