How to check for corrupted DOC and PDF files stored on the server with PHP? -
i have project creating file uploading website university using php. every uploaded file, website must check if uploaded file corrupted or not. i've been searching , found nothing.
for checking whether pdf file corrupted or not.
read first 5 byte of pdf file. if string read %pdf-
, file not corrupted else corrupted.
here working code:
<?php $fp = fopen('mypdffile.pdf', 'r'); // move 0th byte fseek($fp, 0); $data = fread($fp, 5); // read 5 bytes byte 0 if(strcmp($data,"%pdf-")==0) { echo "the pdf file not corrupted."; } else { echo "the pdf file corrupted."; } fclose($fp); ?>
explanation: open non-corrupted file notepad++, notice first 5 byte of opened file equal following sub-string "%pdf-". nothing header format valid pdf file , can take advantage test whether file corrupted or not.
for checking whether .docx file corrupted or not
docx files in zip format, in first 2 bytes letters pk (after zip's creator, phil katz).
so modify above code:
fseek($fp, 0); $data = fread($fp, 2); // read 2 bytes byte 0 if(strcmp($data,"pk")==0) { echo "the docx file not corrupted."; } else { echo "the docx file corrupted."; }
Comments
Post a Comment