【问题标题】:How to check if a PDF file is password protected or not in PHP [closed]如何在PHP中检查PDF文件是否受密码保护[关闭]
【发布时间】:2013-04-09 11:49:22
【问题描述】:

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

【问题讨论】:

    标签: php pdf passwords password-protection


    【解决方案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')));
              }
            }
         }
    

    【讨论】:

    • 感谢您的快速回复,我已经测试过这种方法,但它不能正常工作。请告诉我任何其他方法.........
    【解决方案2】:

    这个问题是is it possible to check if pdf is password protected using ghostscript? 的变体(您可能只需要执行那里提供的shell 脚本,它依赖于ghostcript)

    【讨论】:

    • 您是否尝试过考虑使用 PHP 执行 shell 脚本?很高兴让我知道我的答案被否决的原因。
    • 嗨,shell 我没有拒绝你的回答,但我仍然不明白如何使用你的引用脚本,你能解释一下........
    • 您可以使用本机函数“exec”执行您将在另一个问题的已接受答案中找到的 shell 脚本。为此,在将其路径传递给“exec”之前,您需要对脚本具有执行权限。另一个要求是在您的机器上安装ghostscript,因为它是shell 脚本所需要的。检查“exec”返回的值会告诉您 pdf 文件是否受密码保护。 e.g. $results = array(); exec('path_to_shell_script file.pdf', $results); print_r($results);
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 2016-02-19
    • 2023-03-03
    • 2015-08-24
    • 2013-02-05
    • 1970-01-01
    • 2017-11-01
    • 2023-03-26
    相关资源
    最近更新 更多