I'm uploading some files. Below is part of the script for the upload (I'm using codes from https://github.com/blueimp/jQuery-File-Upload/blob/master/php/index.php)
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
isset($_SERVER['HTTP_X_FILE_NAME']) ?
$_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
isset($_SERVER['HTTP_X_FILE_SIZE']) ?
$_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
isset($_SERVER['HTTP_X_FILE_TYPE']) ?
$_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
$upload['error'][$index]
);
}
When checked, the mime type through $_SERVER['HTTP_X_FILE_TYPE']
or $upload['type'][$index]
returns application/vnd.openxmlformats-officedocument.presentationml.presentation
, application/vnd.openxmlformats-officedocument.wordprocessingml.document
for pptx
, docx
files respectively.
Now after completing the upload, I'm trying to display the files and I'm using finfo_file()
to get the mime type.
$mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_path);
echo $mime_type;
For images files (like png, jpeg) & pdf, the type returns as expected but for the pptx
and docx
files, the mime type returned is application/zip
.
Why is it not returning the same as while in uploading? Is there something wrong with my code or should I expect this? How do I decide which file type it is then?
The same file can have different and mulitple mime-types, that is totally normal.
Additionally the mime-type is only meta-information next to the file itself. Theoretically you can give any file any mime-type. That would not be very useful, but it works. It's just a concept.
The finfo
library will try to obtain the mime-type of a file "magically" by looking into the file trying to identify the format. Then it will return the mime-type according to it's database.
Why is it not returning the same as while in uploading?
The mime-type within the request is given by the HTTP client. It might guess as well, but often it takes the value from information the underlying operating system is giving for that file.
As you can see with your issue that the more common the file-type is, the better it will match (the images).
However as pptx and docx files are actually zip-files, the finfo
library will identify those as application/zip
because the headers of those files (magic numbers) show that it is technically a zip file.
Is there something wrong with my code or should I expect this?
You should not expect that the mime-type of finfo
matches the request header mime-type. Those are two different things.
How do I decide which file type it is then?
That depends. You can decide to trust the http header, you can decide to trust finfo
, you can decide to compare the file extensions as well and a combination of all three.
Additionally you can decide to even add more. This entirely depends on what you do with the uploaded file.