Getting type of mime zip files

Getting the mime type of the downloaded file is quite simple:

echo mime_content_type($fileatt['tmp_name']); 

However, I also want to check the mime files that are included in the zip file. After unpacking my files (scrolling files in zip and where i is the current file), I tried:

 $info = pathinfo($zip->getNameIndex($i)); echo mime_content_type($info['dirname'] . "\\" . $info['basename']); 

which gives the error: Warning: mime_content_type () [function.mime-content-type]: File or path not found '. \ foo.pdf' in C:\Users\<user>\<website>\validation.php in line 56

I realized that the dirname for the archived file refers to the zip file, not the absolute path, so I tried:

 $a = pathinfo($fileatt['tmp_name']); $b = $a['dirname'] . "\\" . $info['basename']; echo mime_content_type($b); 

which gives the error: Warning: mime_content_type () [function.mime-content-type]: File or path not found 'C: \ xampp \ tmp \ foo.pdf' in C:\Users\<user>\<website>\validation.php on line 56

Can anyone shed light on the file path? (I suspect that the answer may coincide with the comment for getting the height and width of the image from zip files , but are there any alternative methods?)

UPDATE

Thanks to Baba, the following works:

 $fp = fopen('zip://C:\Users\<user>\<website>\test.zip#foo.jpg', "r"); 

(nb I can make this work when I give the full route of the zip file, not the tmp file, as it would be when the file is uploaded via the form). However, trying to get the mime type: echo mime_content_type($fp); generates an error:

 Warning: mime_content_type() [function.mime-content-type]: stream does not support seeking in C:\Users\<user>\<website>\includes\validation.php on line 70 

This happens regardless of the type of file (i.e. the problem mentioned in the single comment http://php.net/manual/en/ziparchive.getstream.php does not affect me).

By the way, this is also the same error that I get when I try to use another method: $fp = $zip->getStream('foo.jpg');

I know that on SO there are several other questions not related to the thread, but I could not understand how they related to my problem, and I was hoping that since this method was specifically suggested, someone might have a good answer ...

(ps I do not use the finfo_* function, as my host currently refuses to install PHP v5.3).

+3
php
source share
2 answers

but. You can start by using

  mime_content_type('zip:///path/to/file.zip#'. $chapterZip->getNameIndex ( $i )); 

Q. Now I can only think about replacing mime_content_type , this may not be the best approach, but I'm sure it will be displayed until I figure out the best solution

 $chapterZip = new ZipArchive (); if ($chapterZip->open ( "stockoverflow.zip" )) { for($i = 0; $i < $chapterZip->numFiles; $i ++) { var_dump ( mime_content_type_replacement ( $chapterZip->getNameIndex ( $i ) ) ); } } 

Replace function using file extension and finfo_open ( FILEINFO_MIME )

 function mime_content_type_replacement($filename) { $mime_types = array ( 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', // images 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', // archives 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', // audio/video 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', // adobe 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', // ms office 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', // open office 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet' ); $ext = pathinfo ( $filename, PATHINFO_EXTENSION ); if (array_key_exists ( $ext, $mime_types )) { return $mime_types [$ext]; } elseif (function_exists ( 'finfo_open' )) { $finfo = finfo_open ( FILEINFO_MIME ); $mimetype = finfo_file ( $finfo, $filename ); finfo_close ( $finfo ); return $mimetype; } else { return 'application/octet-stream'; } } 

More mime types

PHP / Mime Types - List of available mime types?

http://snipplr.com/view/1937/

Change 1

Just tested the following and it works

 $fp = fopen('zip://C:\stockoverflow.zip#1.MOV.xml',"r"); 

Edit 2

but. mime_content_type($fp) does not work because mime_content_type only accepts a string argument, see http://php.net/manual/en/function.mime-content-type.php

B. Not sure why you are still sticking to mime_content_type , because it was also depreciated

This feature is deprecated since the PECL Fileinfo extension provides much more efficient functionality (and much more).

C. work on $fileatt['tmp_name'] directly is not ideal .. its temporary file does not need to be manipulated .. in order for you to work on this file, you need to copy it to a server where your PHP will have full permission to access to it

D. ZipArchive::getStream will only work with a local copy of a zip file not a temporary download file

+2
source share

I know that you mentioned that you cannot find the answer for php & lt; 5.3, but I got a little stuck in this and just realized, I hope it will be useful to those who work in the latest version - I could not see it written somewhere else.

Simply put, you can use finfo-> a buffer for binary data to extract a MIME type that is available without extracting the contents to a new file.

 $fileToUnzip = 0; $zip = new ZipArchive; $zip->open("dir/archive.zip"); $binary = $zip->getFromIndex($fileToUnzip); $filename = $zip->getNameIndex($fileToUnzip); $zip->close(); $finfo = new finfo(FILEINFO_MIME_TYPE); $MIMETypeAndCharset = $finfo->buffer($binary); // discard charset info $MIMETypeAndCharsetArray = explode(';', $MIMETypeAndCharset); $MIMEtype = $MIMETypeAndCharsetArray[0]; // you can change the source of an iframe to this php file in order to view a preview // If the browser can't handle the file, the user is presented with a download box header('Content-Type:$MIMEtype'); header('Content-Disposition: inline; filename="July Report.pdf"') echo $binary; 
+2
source share

All Articles