我正在使用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正常工作。我错过了什么吗?
答案 0 :(得分:1)
听起来这是Apache正在运行的用户权限的问题
Finding out what user Apache is running as in windows?
Apache用户至少需要具有对C:\ pdfgenerator
的读取权限