Php learning notes: File Upload (including setting the file upload size limit ),

Source: Internet
Author: User

Php learning notes: File Upload (including setting the file upload size limit ),

Today I wrote about File Upload. I forgot the correct rules I learned a few days ago. I used a very stupid method to judge the format and directly wrote the code:

<? Php/*** Created by PhpStorm. * User: Administrator * Date: 16-9-12 * Time: am ** File Upload **/?> <Form action = "upload. php "method =" post "enctype =" multipart/form-data "> upload files: <input type = "file" name = "file" id = "file"> <br/> <input type = "submit" name = "subbtn" value = "Upload"> </form> <? Phpif (isset ($ _ POST ['subbtn ']) {echo $ _ FILES ['file'] ['type']. "<br/>";/** JPG image/jpeg * GIF image/gif * PNG image/png ** // File Upload handler: // $ _ FILES ['file'] ['type'] type of the uploaded file // $ _ FILES ['file'] ['SIZE'] size of the uploaded file // $ _ FILES ['file'] ['error'] upload error code // $ _ FILES ['file'] ['name'] Upload file name // $ _ FILES [' file '] ['tmp _ name'] temporary file name // $ fileName = ". TXT "; // $ pos = strrpos ($ fileName ,". "); // $ ext = strtolower (substr ($ fileName, $ pos ); // $ File = $ _ FILES ['file']; $ fileName = $ file ['name']; echo 'before suffix cutting :'. $ file ['name']. "<br/>"; // confirm the last one. location: $ pos = strrpos ($ file ['name'], '. '); // convert the file name to lowercase $ ext = strtolower ("$ fileName"); // extract the file name suffix $ fileExten = substr ($ ext, $ pos + 2 ); // when determining the file suffix, it meets the specific requirements. Here it is set: jpg jpeg doc if ($ fileExten = 'jpg '| $ fileExten = 'jpeg' | $ fileExten = 'doc ') {echo "file Suffix :". $ fileExten. "<br/>"; echo "file type :". $ file ['type']. "<br/> "; Echo "file size :". $ file ['SIZE']. "<br/>"; echo "error code :". $ file ['error']. "<br/>"; echo "file name :". $ file ['name']. "<br/>"; echo "temporary file name :". $ file ['tmp _ name']. "<br/>"; move_uploaded_file ($ file ['tmp _ name'], "files /". $ file ['name']);} else echo "incorrect file format" ;}?>

If the format is incorrect, it is intercepted directly.

If you want to modify the size limit of the uploaded file, you can refer to the following practices:

1. General file upload, unless the file is very small. It is like a 5 MB file, it may take more than one minute to complete the upload.
However, in php, the longest execution time of the page is 30 seconds by default. That is to say, the script is stopped after 30 seconds.
This causes the web page to fail to be opened. In this case, we can modify max_execution_time.
Search in php. ini
Max_execution_time
The default value is 30 seconds.
Max_execution_time = 0
0 indicates no limit
2. Modify post_max_size to set the maximum size allowed by POST data. This setting also affects file upload.
The default post_max_size of php is 2 MB. If the size of POST data is larger than that of post_max_size $ _ POST and $ _ FILES superglobals, It is null.
Change to post_max_size.
Post_max_size = 150 M
3. Many people will change the second step. But the maximum size of the file to be uploaded is 8 Mb.
Why? We need to change the parameter upload_max_filesize to indicate the maximum size of the uploaded file.
Find upload_max_filesize. The default value is 8 Mb.
Upload_max_filesize = 100 M
In addition, post_max_size is better than upload_max_filesize.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.