Re: Is this really a pdf?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux