Serious file issues

I am on a shared hosting and fighting with them to get fileinfo to work, we finally got it working, but again I was hit by another barrier, I basically got it because I am creating a new file download and we need to know which mimetypes loaded, but fileinfo does not want to play ball.

So, the .sql file will return the text / plain, correctly.

However, every other file simply returns the application / octet stream, I wonder why this is so, I don’t want you to write a lot of questions about this to the host, so I want to get some research on this issue before I bother them.

the code:

function get_mime($filename) { $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); return $result->file($filename, FILEINFO_MIME_TYPE); } echo $user->get_mime($_FILES['file']['tmp_name'][$i]); 

Any help would be greatly appreciated, thank you very much

Update

So, I went to update the MIMETYPE verification code, and I updated the code to reflect the following (for the function):

 function get_mime($filename) { $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); // return mime type ala mimetype extension if (is_resource($result) === true) { return $result->file($filename, FILEINFO_MIME_TYPE); } else { return "failed"; } return false; } 

Obviously, as you can see, I concluded that it failed when it does not work. This happens 100% of the time, and deleting this if statement causes it to return the application / octet stream, which is incorrect.

The download process will be: User uploads β†’ Files are moved to a temporary folder above the public folder (upload_check), where they will be checked by the file if the file is not what we accept, it will be dropped, otherwise it will be copied to public files and then discarded from temporary folder.

I created this process and still fileinfo does not want to collaborate and returns the application / octet stream for everything, even if they are on a server just saved above the public_html folder.

Any tips?

Jake

+1
php mime fileinfo
source share
2 answers

Tip

I really think you're wrong here ...

but. when loading a file, it also automatically returns mime type to $_FILES

 echo $_FILES['file']['type'][$i] ; 

B, if you want to get mime for a file that has not been downloaded, or try to better determine what the client headers provided, use mime_content_type http://php.net/manual/en/function.mime-content-type.php

 echo mime_content_type($fileName); 

C. Wear

Both mime_content_type and Fileinfo not properly supported, and they have been discounted.

Update: It was once flagged as deprecated in the manual, but was fixed by https://bugs.php.net/bug.php?id=71367 on January 14, 2016. As you can see, support 7.0 does not stop at all.

Alternatively, you can suggest that you look at the following replacement links

Getting type of mime zip files

http://www.php.net/manual/en/function.mime-content-type.php#87856

You can also use getID3 http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.3/ , which can check the real mime type for some common files ...

E. Using Linux command line saver is possible

 $mime = shell_exec("file -bi " . escapeshellarg($file)); 

Or on Mac

 $mime = shell_exec("file -b --mime ".escapeshellarg($file)); 

Finally

Why do you want to work with $_FILES['file']['tmp_name'][$i] directly? It was known to give so many problems.

to try

 $tempDir = ""; // Temp foreach ( $_FILES ["file"] ["error"] as $key => $error ) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES ["file"] ["tmp_name"] [$key]; $name = $_FILES ["file"] ["name"] [$key]; $tempFile = "$tempDir/$name"; move_uploaded_file ( $tmp_name, $tempFile ); echo $user->get_mime ( $tempFile ); unlink ( $tempFile ); } } 
+3
source share

You should check if the magic file you are trying to use exists and can be read in php.

Try:

 $finfo = finfo_open(FILEINFO_MIME, "/usr/share/file/magic.mime"); if (!$finfo) { echo "Opening fileinfo database failed"; exit(); } finfo_close($finfo); 
+2
source share

All Articles