我正在尝试做一个表单,在那里用户可以选择他们想要的PDF小册子,它将被压缩为一个文件名"Brochures.zip“供他们下载。
我有问题要把它拿出来。有人能指出哪里出了问题吗?谢谢!
表格:
<label><input type="checkbox" name="brochure[]" value="file1">file1</label>
<label><input type="checkbox" name="brochure[]" value="file2">file2</label>
<label><input type="checkbox" name="brochure[]" value="file3">file3</label>
<label><input type="checkbox" name="brochure[]" value="file4">file4</label>download.php
$brochure = $_POST['brochure'] ;
$send = true;
if($send) {
$zip = new ZipArchive();
$res = $zip->open('download.zip', ZipArchive::CREATE);
if ($res === TRUE) {
foreach ($brochure as $file => $val) {
$filename = '../pdf/' . $val . '.pdf';
$zip->addFile($filename);
}
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="download.zip"');
readfile('download.zip');
}else {
echo 'failed';
}发布于 2012-06-20 16:49:29
如果文件没有添加到归档中,您可以添加一个简单的检查:
if (!file_exists($filename)) { die("Cannot find $filename"); }您还可以设置错误报告:
error_reporting(-1);
ini_set('display_errors', 'On');然后在$zip->close();之后使用exit;。如果出现任何错误,$zip->addFile()应该会返回false,所以希望它也会显示一些警告/错误。
您也可以使用file_get_contents()来尝试addFromString(),看看这是否有帮助。我的猜测是,包含..的相对路径丢弃了Zip扩展。
发布于 2012-06-20 18:10:43
下面是目录结构。
--文件1.pdf
--文件2.pdf
-- file3.pdf
-- create.php
-- download.php
下面的代码和你的一样……
create.php
<form action="download.php" method="post">
<label><input type="checkbox" name="brochure[]" value="file1">file1</label>
<label><input type="checkbox" name="brochure[]" value="file2">file2</label>
<label><input type="checkbox" name="brochure[]" value="file3">file3</label>
<label><input type="checkbox" name="brochure[]" value="file4">file4</label>
<input type="submit">
</form>download.php与上面的代码相同。确保你有正确的目录结构。正如我所说的,我尝试了你的代码,并且它能像预期的那样工作,用像file1.pdf,file2.pdf等选择的文件创建download.zip文件……
发布于 2012-06-21 09:46:26
我设法通过使用script here解决了这个问题。此外,我意识到我需要将拥有权限的文件夹设置为757,然后才能生成zip文件。
https://stackoverflow.com/questions/11115651
复制相似问题