Principles and implementation of PHP File Upload

Source: Internet
Author: User
Tags form post php file upload
The result is the hidden field of the form option MAX_FILE_SIZE in the file upload.html. You can limit the size of the uploaded file by setting its Value ().

The result is the hidden field of the form option MAX_FILE_SIZE in the file upload.html. You can limit the size of the uploaded file by setting its Value ().

The Value is the hidden Value range of the form option MAX_FILE_SIZE in the file upload.html. You can set the Value to limit the size of the uploaded file.

The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. Therefore, do not forward browser restrictions to this value. In fact, the maximum size of uploaded files in PHP settings will not expire. But it is best to add MAX_FILE_SIZE to the form, because it can avoid the trouble of finding the file too big after you wait for the upload of a large file.

Upload. php $ f = & $ HTTP_POST_FILES ['myfile']; $ dest_dir = 'uploads'; // set the upload directory $ dest = $ dest_dir. '/'. date ("ymd "). "_". $ f ['name']; // set the file name to the date plus the file name to avoid duplication. $ r = move_uploaded_file ($ f ['tmp _ name'], $ dest ); chmod ($ dest, 0755); // sets the attributes of the uploaded file

Or

      

In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile)

$ _ FILES ['userfile'] ['name'] the original name of the client machine file.

$ _ FILES ['userfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/gif ".

$ _ FILES ['userfile'] ['SIZE'] size of the uploaded file, in bytes.

$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.

$ _ FILES ['userfile'] ['error'] error Code related to the File Upload

Value: 0. If no error occurs, the file is 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.

[Edit] parameters involved in PHP File Upload

PHP's default upload limit is 2 MB. to upload files that exceed this setting, you need to adjust some parameters such as PHP and apache. next, we will briefly introduce some parameters involved in PHP file upload:

File_uploads

Whether to allow file upload over HTTP. ON is enabled by default.

Upload_tmp_dir

Upload_tmp_dir indicates the temporary directory where PHP files are uploaded. to upload files, make sure the server does not have the permission to close the temporary files and write the files to the folder. If not specified, PHP uses the system default value.

Upload_max_filesize

Maximum file size that can be uploaded. The default value is 2 MB.

Post_max_size

Controls the maximum data size that PHP can receive in a form submission using the POST method. If you want to use the PHP file upload function, you need to change this value to a value greater than upload_max_filesize.

Max_input_time

The time for receiving data through POST, GET, and PUT is limited in seconds. If the running environment of the application is on the low-speed link, you need to add this value to adapt to the more time required to receive data.

Memory_limit

To avoid using a large amount of available memory by running scripts, PHP allows you to define the memory usage limit. Use the memory_limit variable to specify the maximum memory capacity variable memory_limit that can be used by a single script program. The value of memory_limit should be greater than the value of post_max_size.

Max_execution_time

Max_execution_time sets the time for PHP to wait for the script to be executed before the script is forcibly terminated. The time is calculated in seconds. This variable is useful when the script enters an infinite loop state. However, when there is a legal activity that takes a long time to complete (such as uploading large files), this function will also cause operation failure. In this case, you must consider increasing the value of this variable to prevent PHP from closing the script when the script is executing an important process.

For linux Hosts, the php. conf file may exist under/etc/httpd/conf. d/access. conf/. This file may solve the file size restrictions of some systems.

[Edit] Use discuz to upload large PHP files

The attachment function of the Forum was originally designed not for file management. Due to server configuration, php, network, and other factors, uploading files through the Forum is not a good solution.

If you really need to improve the uploading conditions of Forum attachments, you can try to set the parameters mentioned above in php. ini to meet the needs of uploading large files. At the same time, do not forget to set attachments in the background of the Forum.

There are two main parts of the Forum to limit the size of attachment uploads, from high to low:

Post-related --- attachment type size

User Group --- attachment related

At the same time, the following provides configuration guidance from friends who have successfully uploaded large attachments via http. Of course, due to the different server configurations and network conditions, this does not necessarily apply to your situation. You may need to modify it in many places:

Open php. ini,

Parameter settings

Whether file_uploads on allows file upload over HTTP. ON is enabled by default.

Upload_tmp_dir -- upload the file to the place where the temporary file is stored on the server. If it is not specified, the default Temporary Folder will be used.

Upload_max_filesize 8 m wangwen business, that is, the maximum file size allowed to be uploaded. The default value is 2 MB.

Post_max_size 8 m Refers to the maximum value that can be received by the form POST to PHP, including all values in the form. The default value is 8 Mb.

Description

Generally, after the preceding four parameters are set, uploading a file <= 8 m is not a problem under normal network conditions.

However, if you want to upload a large file larger than 8 Mb, you can only set the above four items. Unless your network has a high upload speed of 100 Mb/s, you must set the following parameters.

Max_execution_time 600 maximum time (in seconds) for running each PHP page. The default value is 30 seconds.

Max_input_time 600 maximum time required for receiving data on each PHP page. The default value is 60 seconds.

Memory_limit 8 m maximum memory consumed by each PHP page. The default value is 8 M.

After modifying the preceding parameters, you can upload a large volume of files as permitted by the network.

[Edit] common error types for uploading Forum files (keep summing up ...)

Warning: Unable to open '\ php2' for reading: Invalid argument in e: \ user \ web \ larksoft.net \ upload \ upfile. php on line 10

Is the reason for php's upload_tmp_dir. The specified directory must be readable and writable.

Parse error: parse error in c: \ program files \ apache group \ apache \ htdocs \ mdweb \ ftpfile \ upload. php on line 14

Parse errors are generally statements, such as ";", "", and ")".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.