Solved

Check to see if file is corrupt

Posted on 2009-04-08
4
297 Views
Last Modified: 2012-05-06
I have a PDF I am creating using TCPDF then saving to a temp pdf file. I am then sending out the PDF via mailer class. 1 out of 50 files or so send a corrupt file. Is there a way to test a stored PDF to see if the document is not corrupt?
0
Comment
Question by:nbcit
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
EMB01 earned 500 total points
ID: 24098532
Notice the following code simply tries to read the file and if an error occurs (because it's corrupt) PHP returns false and the $error is set.
<?php

$file = fopen("welcome.txt", "r") or exit("Unable to open file!");

if ($file == FALSE) {

$error = 1;

}

//Output a line of the file until the end is reached

while(!feof($file))

  {

  echo fgets($file). "<br />";

  }

fclose($file);

?>

Open in new window

0
 

Author Comment

by:nbcit
ID: 24099062
So the error would occur at fopen and then it will return false.
<?php

$file = fopen($pdf_path, "r");

if ($file === false) die('this is an error');

fclose($pdf_path);

?>

Open in new window

0
 
LVL 14

Expert Comment

by:EMB01
ID: 24099106
I believe so. You may also want to check this out if you are creating PDF files that are corrupt. It seems you must use the -b flag for Windows...
http://bugs.php.net/bug.php?id=21904
0
 

Author Comment

by:nbcit
ID: 24099505
It turns out that if I unlink the file prior to file_put_contents the files come out perfect. Thank you.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now