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.

enter image description here

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

enter code here

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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -