This article mainly explains the knowledge points related to the configuration of PHP file upload. The PHP file upload function configuration mainly involves the upload_tmp_dir, upload_max_filesize, post_max_size and other options in the php.ini configuration file, which are explained one by one below.
Open the php.ini configuration file and find File Uploads
file_uploads = On
HTTP file uploads are allowed by default, this option cannot be set to OFF.
upload_tmp_dir =
The default is empty. This option is 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 a value to this option, such as upload_tmp_dir ='/leapsoulcn', which means there is a leapsoulcn directory in the C drive directory. It is the same as the session configuration. If you are in a Linux environment, you must give this directory writable permissions.
How to upload a large file exceeding 8M?
Uploading large files mainly involves configuring two options: upload_max_filesize and post_max_size.
The default file upload size in the php.ini configuration file is 2M. A mistake that PHP beginners often make is to set the form area for the maximum size of the uploaded file when writing the file upload function, that is, the maximum value of the file allowed to be uploaded, max_file_size (hidden Value range) to specify the size of the uploaded file. In fact, generally others can bypass this value, so for safety reasons, it is best to configure the upload_max_filesize option in the php.ini configuration file to set the size of the file upload.
The default upload_max_filesize = 2M, that is, the file upload size is 2M. If you want to upload a file exceeding 8M, such as 20M, you must set upload_max_filesize = 20M .
However, just setting upload_max_filesize = 20M still cannot realize the upload function of large files. You must modify the post_max_size option in the php.ini configuration file, which represents the maximum byte length of POST data allowed, and the default is 8M. If the POST data exceeds the limit, $_POST and $_FILES will be empty. To upload large files, you must set the value of this option to be greater than the value of the upload_max_filesize command. I usually set the upload_max_filesize and post_max_size values to be equal. In addition, if memory limit is enabled, this value should be smaller than the value of the memory_limit option.
Other notes on 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 that the script execution exceeds 30 seconds. This is because in the php.ini configuration file max_execution_timeThe configuration option is causing trouble, it indicates the maximum allowed execution time (in seconds) of each script, 0 means no limit. You can adjust the value of max_execution_time appropriately. Setting it to 0 is not recommended.
At this point, the PHP tutorial for configuring the file upload options in the php.ini configuration file has been introduced. Through the practice and learning of the above steps, combined with the PHP program, the file upload function can be realized.