PHP implementation of file fragment upload example code


Super global variable array for PHP$_ Files to record the information related to file upload.


Allow uploading files via HTTP


The maximum execution time of the script is allowed. If it exceeds this time, an error will be reported


Set the maximum amount of memory that a script can allocate to prevent runaway scripts from occupying too much memory. This instruction is only set at compile time.

–It takes effect only when the enable memory limit flag is used


The maximum size of the file allowed to be uploaded. This instruction must be less than post_ max_ size


Temporary storage directory of uploaded files


Allow post mode to accept the maximum size

$_ Files [‘myfile ‘] [‘name’] the original name of the last file on the client.

$_ The MIME type of files [‘myfile ‘] [‘type’] file needs the support of the browser, such as “image / GIF”.

$_ Files [‘myfile ‘] [‘size’] the size of the uploaded file, in bytes.

$_ The temporary file name stored on the server after the files [‘myfile ‘] [‘tmp_name’] file is uploaded, which is generally the system default. You can upload in php.ini_ tmp_ Dir is specified, but setting it with the putenv() function has no effect.

$_ Files [‘myfile ‘] [‘error’] and the status code related to the file upload. [‘error ‘] was added in PHP version 4.2.0. The following is its description: (they became constants after php3.0)


Value: 0; No error occurred and the file was uploaded successfully.


Value: 1; The uploaded file exceeds the upload in php.ini_ max_ The filesize option limits the value of the.


Value: 2; The size of the uploaded file exceeds Max in the HTML form_ FILE_ The value specified by the size option.


Value: 3; Only part of the file was uploaded.


Value: 4; No files were uploaded.

Value: 5; The upload file size is 0

After the file is uploaded, it is stored in the temporary directory by default. At this time, you must delete it from the temporary directory or move it to other places. If not, it will be deleted.

That is, whether the upload is successful or not, the files in the temporary directory will be deleted after the script is executed.

Attachment: how to modify the size limit of PHP uploaded files

1. General file upload, unless the file is very small. Like a 5m file, it may take more than a minute to upload.

However, in PHP, the default maximum execution time of this page is 30 seconds. That is, after 30 seconds, the script stops executing

This leads to the situation that the web page cannot be opened. At this time, we can modify max_ execution_ time

Find it in php.ini


The default is 30 seconds. Change to

max_execution_time = 0

0 means there is no limit

2. Modify post_ max_ Size sets the maximum size allowed for post data. This setting also affects file upload.

PHP default post_ max_ The size is 2m. If the post data size is greater than post_ max_ size $_ Post and$_ Files Superglobals will be empty

Find post_ max_ Size. Replace with

post_max_size = 150M

3. Many people will change the second step, but the maximum is still 8m when uploading files.

Why? We need to change a parameter, upload_ max_ Filesize indicates the maximum size of the uploaded file.

Find upload_ max_ Filesize, which is 8m by default and changed to

upload_max_filesize = 100M

Another thing to note is that post_ max_ Size greater than upload_ max_ Filesize is better

Upload effect display:

If you have any additional knowledge points, you can contact Xiaobian and thank you for your support for developeppaer.