如何在PHP中检查PDF文件是否受密码保护

3

我如何在使用PHP上传多个文件时检查上传的PDF文件是否受密码保护?如果是受密码保护的,我想显示一个错误提示。

2个回答

1
我在网上找到了一篇类似的文章,看看这个链接:http://drupal.org/node/843516
/**
* 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')));
          }
        }
     }

谢谢您的快速回复,我已经测试了这种方法,但它并不能正常工作。请告诉我是否有其他方法...... - Fairoz

0

你尝试过使用PHP执行shell脚本吗?能否告诉我为什么我的回答被踩了呢? - Thierry M.S.
嗨,Shell,我没有给你的答案投反对票,但我仍然不明白如何使用你所提到的脚本,请你能否解释一下…… - Fairoz
您可以使用本地函数“exec”执行在其他问题的已接受答案中找到的shell脚本。为此,在将其路径传递给“exec”之前,您需要对脚本具有执行权限。另一个要求是在您的计算机上安装ghostscript,因为shell脚本需要它。通过检查“exec”返回的值,可以告诉您pdf文件是否受密码保护。例如:$results = array(); exec('path_to_shell_script file.pdf', $results); print_r($results); - Thierry M.S.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接