Re: Is this really a pdf?
| From: | Colin Guthrie | Date: | Mon, 07 Aug 2006 14:21:39 +0000 |
| Subject: | Re: Is this really a pdf? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240255@lists.php.net to get a copy of this message | ||
Sjef wrote:
Is it possible to recognize if a file for upload really is a pdf (like the function getimagesize retuns the file type of the image)?If you have a suitible mime.magic file, and your PHP has the functionality built into it, you could try the mime_content_type() function. http://uk.php.net/manual/en/function.mime-content-type.php Or if not available, read the first few bytes of the file and verify that it begins with: %PDF-1.3 (or other versions - perhaps just verify the first 4 chars. You can do this with a simple $fp = fopen($filename, 'rb')); if ('%PDF' == fread($fp, 4)) // PDF else // Not PDF This is very simple. You could also use the "file" commandline utility if the server is a *nix machine and parse it's output. e.g. on my machine: [colin@jimmy www]$ file ~/Desktop/svn-book.pdf /home/colin/Desktop/svn-book.pdf: PDF document, version 1.3 or easier: [colin@jimmy www]$ file -i ~/Desktop/svn-book.pdf /home/colin/Desktop/svn-book.pdf: application/pdf Col.