How can I check if uploading a PDF file is password protected or not while uploading multiple files using PHP? I want to show an error if it's password protected.
Asked
Active
Viewed 6,082 times
2 Answers
2
i found a similiar post on the web check it out.
/**
* Check whether pdf is encrypted or password protected.
* @param <type> $form
* @param <type> $form_state
*/
function pdftest_is_encrypted($form,&$form_state) {
include_once 'sites/all/libraries/fpdi/pdf_parser.php';
foreach($form_state['values']['files'] as $value) {
//Check whether file type is pdf and confirm the file is selected to remove.
if($value['filemime'] == 'application/pdf' && $value['remove'] != 1) {
$pdf = new pdf_parser($value['filepath']);
if(stristr($pdf->errormsg,'File is encrypted')) {
form_set_error('field_attachment', t('Uploaded PDF Document '.$value['filename'].' is encrypted and can not be uploaded. '.l('Guide to troubleshooting failed uploads.','http://support.scribd.com/forums/33627/entries/24412')));
}
}
}

JSJ
- 5,653
- 3
- 25
- 32
-
Thanks for the quick reply, I have tested this method but it's not working fine. please tell me any other method......... – Fairoz Apr 09 '13 at 12:14
0
This question is a variation of is it possible to check if pdf is password protected using ghostscript? (you might just need to execute the shell script provided there, which relies on ghostcript )
-
Have you tried considering the execution of the shell script using PHP? It would be nice to let me know the reason my answer has been downvoted. – Th. Ma. Apr 09 '13 at 12:20
-
Hi shell i didn't downvoted to your answer but still i didn't understand the how to use the your refers script, can you please explain........ – Fairoz Apr 09 '13 at 12:55
-
You can execute the shell script you'll find in the accepted answer of the other question by using native function "exec". To do so, you need execution permission on the script before passing its path to "exec". Another requirement would be to have ghostscript installed on your machine as it is required by the shell script. Checking the value returned by "exec" would tell you if a pdf file is password protected or not. `e.g. $results = array(); exec('path_to_shell_script file.pdf', $results); print_r($results); ` – Th. Ma. Apr 09 '13 at 13:47