Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 415
phpdeveloper Avatar asked Dec 18 '22 12:12

phpdeveloper


1 Answers

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).

like image 86
Plycoder Avatar answered Feb 02 '23 00:02

Plycoder