Solved

Check to see if file is corrupt

Posted on 2009-04-08
4
300 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress errors showing up, hacked? 4 35
Advice and best practice  SQLite3 Database using PHP 2 49
mysqli insert query problems 4 22
error log using ftp 7 41
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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