How can I limit the size of uploaded files by configuring PHP files?
In the process of website development, in order to ensure the full use of the server space, in the development of the upload function, the upload file must be the size of control. So how do we control the size of the uploaded file?
The size of the control file can be started in two ways:
The first is to control the upload file in PHP configuration file php.ini, if the upload file exceeds its specified range, then the execution of the upload will be recognized.
The second is within the scope of the PHP configuration file allowed, in the program to upload the size of the file control!
One: Upload files via profile control
PHP through the php.ini file to control the upload file, including whether to support the upload, upload files temporary file directory, upload file size, instruction execution time, instruction allocated memory space.
In php.ini, define the File uploads entry to complete the settings for the upload-related options. The upload-related options have the following meanings:
file_uploads: If the value is no, the server supports uploading the file and if it is off, it is not supported. General default is supported, this does not have to modify!
Upload_tmp_dri: Upload File temp directory. 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 set it yourself!
upload_max_filesize: The server allows to upload the maximum value of the file, in megabytes, the system defaults to 2MB, if the site needs to upload more than 2MB of data, then you need to modify this value!
The above is a description of the relevant parameter set in PHP.ini in File_uploads, in addition to the contents of the File_uploads item, there are several other options in php.ini that affect file uploads ~
Max_execution_time: The maximum time, in seconds, that an instruction in PHP can perform. This option must be modified when uploading large files, or even if the upload file is within the scope allowed by the server, but if it exceeds the maximum time that the instruction can execute, it will not be able to implement the upload ~
Memory_limit: The amount of memory space allocated by an instruction in PHP, in megabytes. Its size will also affect the upload of super-large files!
Second: Control the uploading of files in the program
The enctype and method attributes in the form form and the hidden field max_file_size are applied to the client control upload file.
enctype = "Multipart/form-data": specifies how the form encodes data.
method = "POST": specifies how the data is transferred.
<input type = "hidden" name= "max_file_size" value= " 10000"/>: Control the size of the uploaded file (in bytes) by hiding the domain, which cannot exceed the php.ini configuration file The upload_max_filesize option sets a value that does not drink full control over the size of the uploaded file. Just can avoid some unnecessary trouble.
Skills:
When you apply a hidden field <input type = "hidden" name= "max_file_size" value= "10000"/> control file upload size, you must place it before the file field, otherwise it will not work!