SoFunction
Updated on 2025-03-02

What do you have to know when uploading php file

Open the configuration file and find itFile Uploads
file_uploads = On

HTTP file uploads are allowed by default, and this option cannot be set to OFF.
upload_tmp_dir =
The default is empty. This option is also easy to forget when manually configuring the PHP running environment. If this option is not configured, the file upload function cannot be implemented. This option sets the temporary directory where files are stored when uploading files. You must assign values ​​to this option, such as upload_tmp_dir ='/leapsoulcn', which means there is a leapsoulcn directory in the C disk directory. The same as the session configuration. If you are in the Linux environment, you must give this directory writable permissions.
How to upload large files over 8M?
Uploading large files mainly involves configurationupload_max_filesizeandpost_max_sizeTwo options.
The default file upload size in the configuration file is 2M. A mistake that beginners of php are prone to make is to set the form area of ​​the maximum size of the upload file when writing the file upload function, that is, the maximum value of the upload file allowed, and the value of max_file_size (hidden value range) to specify the size of the upload file. In fact, others can generally bypass this value, so for security reasons, it is best to configure the upload_max_filesize option in the configuration file to set the size of the upload file.
defaultupload_max_filesize = 2M, that is, the file upload size is 2M. If you want to upload files that exceed 8M, such as 20M, you must set itupload_max_filesize = 20M
However, if you set upload_max_filesize = 20M, you still cannot achieve the upload function of large files. You must modify the configuration filepost_max_sizeOption, which represents the maximum byte length of data allowed for POST, defaults to 8M. If the POST data exceeds the limit, then $_POST and $_FILES will be empty. To upload a large file, you must set the value of the option greater than the upload_max_filesize directive.I usually set the upload_max_filesize and post_max_size values ​​to be equal. Also, if memory limit is enabled, the value should be less than the value of the memory_limit option.
Other notes for file upload
When uploading large files, you will feel that the upload speed is slow. When it exceeds a certain time, an error will be reported for script execution for more than 30 seconds. This is because in the configuration filemax_execution_timeConfiguration options are at the verge of, which indicates the maximum allowable execution time (seconds) per script, and 0 indicates no limit. You can adjust the value of max_execution_time appropriately, and it is not recommended to set it to 0.
At this point, the PHP tutorial for configuring file upload options in the configuration file has been introduced. Through the above steps, practice and learning, and combined with the PHP program, the file upload function can be realized.