Home > Article > Backend Development > How to limit the size of uploaded files by configuring php files?
How to limit the size of uploaded files by configuring php files?
In the process of website development, in order to ensure that the server space can be fully utilized, the size of the uploaded file must be controlled when developing the upload function. So how do we control the size of uploaded files?
Controlling the size of the file can be done from two aspects:
The first is to control the uploaded file in the PHP configuration file php.ini. If the uploaded file exceeds its specified range , then the upload will be recognized.
The second is to control the size of the uploaded file in the program within the scope allowed by the PHP configuration file!
1: Control uploaded files through configuration files
# In PHP, uploaded files are controlled through the php.ini file, including whether uploading is supported. The temporary file directory of the uploaded file, the size of the uploaded file, the instruction execution time, and the memory space allocated by the instruction.
In php.ini, define the File Uploads item to complete the settings of upload-related options. The meanings of upload related options are as follows:
file_uploads: If the value is no, it means that the server supports uploading files. If it is off, it does not support it. Generally, it is supported by default, and there is no need to modify this!
upload_tmp_dri: Temporary directory for uploading files. Before the file is successfully uploaded, the file is first stored in a temporary directory on the server side. Most use the system default directory, but you can also set it yourself!
upload_max_filesize: The maximum value of files allowed to be uploaded by the server, in MB, the system default is 2MB. If the website needs to upload data exceeding 2MB, then this value must be modified!
The above is the description of the parameter settings related to the above in the File_Uploads item in php.ini. In addition to the content in the File_Uploads item, there are several other options in php.ini that will affect the upload of files~
max_execution_time: The maximum time that a command can be executed in PHP, in seconds. This option must be modified when uploading very large files. Otherwise, even if the uploaded file is within the range allowed by the server, if it exceeds the maximum execution time of the command, the upload will still not be possible~
memory_limit: The memory space allocated by an instruction in PHP, the unit is MB. Its size also affects the uploading of very large files!
2: Control the uploaded file in the program
To control the uploaded file on the client side, the enctype and method attributes in the form form and Hidden field MAX_FILE_SIZE.
enctype = "multipart/form-data": Specify the form encoding data method.
method = "post": Specify the data transmission method.
: Control the size of the uploaded file (in bytes) through the hidden field. The value cannot exceed the value set by the upload_max_filesize option in the php.ini configuration file. It does not fully control the size of the uploaded file. Just to avoid some unnecessary trouble.
Tips:
When applying the hidden field to control the file upload size, it must be placed in before the file domain, otherwise it will not work!
The above is the detailed content of How to limit the size of uploaded files by configuring php files?. For more information, please follow other related articles on the PHP Chinese website!