【问题标题】:PHP ZipArchive::AddFile from different directory issue来自不同目录问题的 PHP ZipArchive::AddFile
【发布时间】:2013-01-24 20:32:08
【问题描述】:

我正在使用 PHP 的 ZipArchive 类将生成的 PDF 文件添加到 zip 存档中。生成的 PDF 存储在 C:\pdfgenerator (不是我的 www 目录)中,而 zip 文件本身创建并存储在 C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\med (我的网站所在的目录生活)。

这是我用来创建 PDF/Zip 并尝试将 PDF 存储在 Zip 中的代码:

/* build zip & directory for PDFs */
$zip = new ZipArchive;
$time = microtime(true);

if($res = $zip->open("pdf/mf-pdfs_" . $time . ".zip", ZipArchive::CREATE) === TRUE) {
    echo "created";
    } else {
    echo "failed";
    }


$num = 0;
while($row = mysql_fetch_array($result)) {

    /* associate a random # assigned to each PDF file name */
    $num++;
        include($form);
    $rand = rand(1,50000);
     $file_num = $num * $rand;

    $fo = fopen('c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.html', 'w') or die("can't open file");

    fwrite($fo, $output);
    echo shell_exec('c:\wkhtmltopdf\wkhtmltoimage c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.html c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.jpeg');

    /* the follow uses ghost script to execute the ImageMagick convert command from cmd.exe */
    $magick_dir = 'C:\imagemagick' ; // install IM in short DOS 8.3 compatible path
    $send_cmd=$magick_dir .'\convert c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.jpeg -resize "1710x2200^!" c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.jpeg' ;

    $result = shell_exec($send_cmd);
    $send_cmd=$magick_dir .'\convert c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.jpeg c:\\pdfgenerator\\mf_pdf-' . $time . '-' . $file_num . '.pdf';
    $result = shell_exec($send_cmd) ;

    /* EO ghostscript code */

/* add the newly generated files to the Zip Archive */
    if ($res === TRUE) {
    //echo "RESULT TRUE...";
     if($zip->addFile('c:/pdfgenerator/mf_pdf-' . $time . '-' . $file_num . '.pdf','c:/pdfgenerator/mf_pdf-' . $time . '-' . $file_num . '.pdf') === TRUE) {
        echo "addded";
        } else {
        echo "not added";
        }
    //echo "FILE ADDED!";
    } 
 /* EO Zip Archive */

}

当我将 PDF 和 Zip 文件存储在同一个父目录 (/med) 中时,脚本运行良好。

是否可以使用 Zip 存档类并将不在同一父目录中的文件添加到 Zip?我尝试了许多更改,但无法让 AddFile 正常工作。我错过了什么吗?

【问题讨论】:

    标签: php imagemagick wkhtmltopdf zipfile


    【解决方案1】:

    听起来像是运行 Apache 的用户的权限问题

    Finding out what user Apache is running as in windows?

    Apache 用户至少需要对 C:\pdfgenerator 具有读取权限

    【讨论】:

    • 感谢您指出它可能是权限问题。由于我手动创建了 c:\pdfgenerator 而不是通过 PHP 和 mkdir() 功能,因此未授予 internet/apache 用户修改权限。将互联网用户的权限设置为“修改”成功解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多