phpdeveloper phpdeveloper - 2 months ago 20
PHP Question

Using php how I check a pdf file contents is valid or invalid

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:


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";
                       echo "In Valid pdf";

Note: Pdf version could be different such 1.0 , 1.5 , 1.7 etc... In may case it was 1.5