What are the ways to detect whether a file is a compressed file, while writing a upload script in PHP?
-
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) This way you can detect whether a file being uploaded is an Image. I want to know the ways to detect whether a file is a zip or rar file.
-
Answer:
Unorthodox a bit, but probably a good way would be to execute FILE(1) shell command in order to check what the file really is. https://en.wikipedia.org/wiki/File_(command)
Vedran Krivokuca at Quora Visit the source
Other answers
From what i understand, you need to detect a file of type zip or rar, image/gif would be the mime of a gif file, so you need to look for mime types like: application/zip application/x-rar-compressed A good mime types reference can be found in wikipedia: http://en.wikipedia.org/wiki/Internet_media_type And here: http://www.hansenb.pdx.edu/DMKB/dict/tutorials/mime_typ.php Also, for PHP you might also look into: http://php.net/manual/en/function.mime-content-type.php this function, notice it's part of the Fileinfo package, not always compiled with default PHP. and this PEAR package too: http://pear.php.net/package/MIME_Type
Igal Alkon
You can check the file type and, if it's a zip file, use http://www.php.net/manual/en/function.zip-read.php to look inside it. Call zip_open() first, then call zip_read() to look at each file in the archive and check its type. I just searched for a similar ability with rar files and found http://www.php.net/manual/en/class.rararchive.php It's setup a little differently, as a class rather than global functions, but appears to be the same idea. I guess the difference is because the zip functions predate OO support in PHP. Also, it looks like this rar functionality may not be in a released version of PHP yet.
David Albert
You can check the extension of the file and also the MIME type of the file to check whether the file is a compressed file. But dont forget that they can be spoofed easily.
Robin Thomas
This is tagged under cakePhp. There is an incredibly well written library called http://milesj.me/code/cakephp/uploader which will save the MIME type for you, and you need not do any explicit "uploading" as it is augmented as a behavior and a simple model->save() will save the file (including transporting it to a 3rd party provider like Amazon S3). As mentioned already, use the appropriate MIME to detect the file. extensions are unreliable and can lie. If you want to however detect it at the client-side, before any uploading to the server happens (again not reliable, because a browser can lie) then you can use the html5 http://dev.w3.org/2006/webapi/FileAPI/ to detect it. Again purely using this api means you take a hit on cross browser compatibility.
Ankan Adhikari
Related Q & A:
- How do I convert a PDF file to PDF/A in Delphi?Best solution by softwarerecs.stackexchange.com
- How to check whether a cell in a DataGridView is changed or not?Best solution by Stack Overflow
- How to call a function with parameter in a bash script?Best solution by tldp.org
- What are some ways I can make a little money?Best solution by Yahoo! Answers
- How do you unzip a compressed file?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.