PHP has been writing PHP file upload code over the past few days to determine the type of files to be uploaded. the BUG has been fixed by itself. the only unsolved problem is:
Determine the type of the uploaded file. Most of the methods mentioned on the Internet are to determine the file extension or type ($ _ FILES ['file'] ['type']). However, if someone changes the extension, they can upload the file. So I do not use this method.
Later I found a function:
Function checkTitle ($ filename) // judge the file type {$ filename = ($ _ FILES ['file'] ['tmp _ name']); $ 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 8075: $ fileType = 'Zip'; 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;} // Fixif ($ strInfo ['chars1'] = '-1' & $ strInfo ['chars2'] ='-40 ') {return 'jpg ';} if ($ strInfo ['chars1'] ='-119 '& $ strInfo ['chars2'] = '80 ') {return 'PNG ';} return $ fileType ;}
This function can determine the actual file type, but when I call the function, use:
Comment ');
If (! In_array (checkTitle ($ _ FILES ['file'] ['tmp _ name'])
, $ Type) // determine the type of the uploaded file
{
// Prompt for incorrect file type
}
If it is $ _ FILES ['file'] ['name'], an error is returned, if it is $ _ FILES ['file'] ['tmp _ name'], the system will prompt that the file type is incorrect (even if the image and rarfile I uploaded are the same ), how can I write it correctly?
Reply to discussion (solution)
First print out the returned value of the checkTitle method.
How can I add $ filename = ($ _ FILES ['file'] ['tmp _ name']) in the checkTitle method?
1,
Function checkTitle ($ filename) // determines the file type
{
$ Filename = ($ _ FILES ['file'] ['tmp _ name']);
Ying writing
Function checkTitle ($ filename) // judge the file type {$ filename = $ filename ['tmp _ name']);
2,
If (! In_array (checkTitle ($ _ FILES ['file'] ['tmp _ name']), $ type) // you can specify the object type.
{
Ying writing
If (! In_array (checkTitle ($ _ FILES ['file']), $ type) // determines the type of the uploaded file {
Because you cannot assume that all form controls are named as files. what if they are other names?
1,
Function checkTitle ($ filename) // determines the file type
{
$ Filename = ($ _ FILES ['file'] ['tmp _ name']);
Ying writing
PHP code? 123 function checkTitle ($ filename) // judge the file type {$ filename = $ filename ['tmp _ n ......
The file uploaded in my form is called "file ":
First print out the returned value of the checkTitle method.
How can I add $ filename = ($ _ FILES ['file'] ['tmp _ name']) in the checkTitle method?
If I write $ filename = ($ _ FILES ['file'] ['name']), an error is returned, so I have to change the name to tmp_name.
Solved the problem by yourself:
1. because temporary files have not been moved yet, you can only use tmp_name. if you use name, you cannot obtain the result (the tmp_name file is on the server, and the name file is on the client );
2. the reason cannot be determined is that the values in array () have ".", and you can delete it:
Comment ');
(Because I started to use methods based on the extension, there are ".")
Now the file upload code is completely normal. In addition, I can see another code on the internet. take the first four digits of the file header and convert it to hexadecimal.