teamfox20 wrote:Can someone show me how to add a restriction to files uploaded to the code below. I only want PDF files to be uploaded.
Those scripts that make a true detection of mimi type is using same technique as Virus software.
They look for some unique characters, code in side a file to see what type it is.
It is a FingerPrint.
These fingerprints are told by definition files, which are updated often.
When it comes to PDF this is the beginning of a typical pdf file
5 first characters only
%PDF-
And here is the line used by a mime type detection script,
in definitions of file types
0 string %PDF- application/pdf
0 ....... means start looking at position 0 = the beginning
string ..... means look for a string, not a number
%PDF- ...... is the string to look for, The FingerPrint
application/pdf ...... is the type to return
When we know this,
we can easily try to see what type a file is:
<?php
// PDF check script, by halojoy 2007
$file = 'somefile.pdf';
// string to look for
$pdf_str = '%PDF-';
// open file and read first 5 chars
$fh = fopen($file, 'r');
$file_str = fread($fh, 5);
// compare
if($file_str == $pdf_str)
echo 'Is PDF, yes';
else
echo 'Not PDF, no';
?>
How sure can we be this is a PDF?
I guess nothing is 100%.
😉
🙂
Regards, halojoy