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.