Upload using PHP file functions
This sectionCodeDivided into two files, one is upload.html and the other is upload. php.
<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 File"> </form>
Where,
- Please note that <form enctype = "multipart/form-Data" ......> this is a tag. to upload a file, you must specify it as multipart/form-Data. Otherwise, the server will not know what to do.
- 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.
$ F = & $ http_post_files ['myfile']; $ dest_dir = 'uploads'; // sets 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
<? Copy ($ _ FILES [myfile] [tmp_name], $ _ FILES [myfile] [name]);?>
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 file upload in PHP
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:
Whether to allow file upload over HTTP. On is enabled by default.
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.
Maximum file size that can be uploaded. The default value is 2 MB.
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.
The time for receiving data through post, get, and put is limited in seconds. If the applicationProgramWhen the running environment is on the low-speed link, you need to add this value to adapt to the more time required to receive data.
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 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 PHP large 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,
| Parameters |
Set |
Description |
| File_uploads |
On |
Whether to allow file upload over HTTP. On is enabled by default. |
| Upload_tmp_dir |
-- |
The file is uploaded 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 |
The maximum value that can be received by posting a form 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 Mb. |
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 ")".