PHP file Upload type thoroughly judgment scheme and PHP + nginx file upload size thoroughly control scheme

Source: Internet
Author: User
Tags php file upload
This article aims to further correct the mime judgment method described above and add the code affected by the file upload size in an nginx environment

The purpose of this article is to further correct the mime judgment method described above and add the code affected by the file upload size in an nginx environment.

Upload type control:

I found in my (54 chen) work that, in fact, modifying the file suffix, the browser will be very foolish to transfer the wrong mime type, therefore, the previous judgment is a semi-incorrect method (except that the C code is correct ).

There are some bugs in the method for judging the file type by reading the file header from PHP on the Internet. I (54 chen) modified and tested it. it should look like this:

/**

* Determine the file type by reading the first few bytes of the file

*

* @ Return String

*/

Function checkTitle ($ filename ){

$ File = fopen ($ filename, "rb ");

$ Bin = fread ($ file, 2); // read-only 2 bytes

Fclose ($ file );

$ StrInfo = @ unpack ("c2chars", $ bin );

$ TypeCode = intval ($ strInfo [\ 'chars1 \ ']. $ strInfo [\ 'chars2 \']);

$ FileType = ";

Switch ($ typeCode)

{

Case 7790:

$ FileType = 'exe ';

Break;

Case 7784:

$ FileType = 'midi ';

Break;

Case 8297:

$ FileType = 'rar ';

Break;

Case 255216:

$ FileType = 'jpg ';

Break;

Case 7173:

$ FileType = 'GIF ';

Break;

Case 6677:

$ FileType = 'bmp ';

Break;

Case 13780:

$ FileType = 'PNG ';

Break;

Default:

$ FileType = 'unknown '. $ typeCode;

}

// Fix

If ($ strInfo [\ 'chars1 \ '] ='-1' & $ strInfo [\ 'chars2 \ '] ='-40 ′){

Return 'jpg ';

}

If ($ strInfo [\ 'chars1 \ '] ='-119 '& $ strInfo [\ 'chars2 \'] = '80 ′){

Return 'PNG ';

}

Return $ fileType;

}

This code correctly separates the modified file to prevent modification of the suffix Upload.

Upload size control:

Directly read the size of $ _ FILE in PHP code. if a large FILE is used with PHP + nginx, it is very likely that, if the file size exceeds 2 MB, nginx will throw the 413 error.

Solution:

Modify/etc/nginx. conf

Find the server segment of the corresponding domain name: modify the value of client_max_body_size. the default value is 2 MB.

This is not enough. if you do not modify the value in php. ini, you will find that problems may occur when you use the code above to determine the type of the uploaded file.

Modify/etc/php. ini

Find upload_max_filesize and modify this value. The default value is 2 MB.

After a while, the control of the type and size is basically complete.

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.