I am trying to implement a functionality that should be to detect pdf file and it's content is valid or invalid. Using following scripts I can easily detect whether file is pdf or not:
$info = pathinfo("test.pdf");
if ($info["extension"] == "pdf"){
echo "PDF file";
}
Now I want to check if a file extension pdf then content of pdf file should be valid.
Please tell how can I check pdf file contents are valid not corrupted or invalid format.
Content of pdf file start with %PDF-version no, So at first get contents of pdf file using following scripts:
$filecontent = file_get_contents("test.pdf");
After that check $filecontent variable using following regular expression in order detect it's valid or invalid format:
if (preg_match("/^%PDF-1.5/", $filecontent)) {
echo "Valid pdf";
} else {
echo "In Valid pdf";
}
Note: Pdf version could be different such 1.0 , 1.5 , 1.7 etc... In my case it was 1.5 also make sure you have placed above code inside of scripts/conditions (if file has .pdf extension).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With