当我尝试使用我需要做的搜索解决方案时,我似乎找不到任何有用的工作。
我正在尝试创建一个表单,用户将在网页上选择1个或更多(批量选择全部)pdf文件。
当用户点击提交时,我想让我的PHP下载文件创建一个zip文件,将选定的pdf文件(存储在服务器上)添加到zip文件,然后强制将zip文件下载到最终用户。
当用户打开zip文件时,他们从网页表单中选择的pdf文件可供他们使用。
我找到的所有内容要么不起作用,要么不安全,要么首先上传我不想做的文件。这些文件已存储在我的服务器上。
非常感谢任何帮助。
<code>
$files = array($_POST['file']);
$zip = new ZipArchive();
$filename = "practiceforms";
$zip->open($filename, ZipArchive::CREATE);
foreach ($_POST['file'] as $key => $val) {
echo $path = "/images/docs/solutions/".$val.".pdf";
if(file_exists($path)){
$zip->addFile(basename($path), file_get_contents($path));
//$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
echo header('Content-Length: ' . filesize($filename));
exit;
</code>
答案 0 :(得分:1)
这是我的最终工作版本 - 如果您有任何关于简化它的建议,请告诉我。另外,非常感谢您的支持和意见。
<code>
<form>
<a href="#" name='checkall' onclick='checkedAll(jacheckscript);'>Check/Uncheck All</a>
<ul>
<li><input type="checkbox" class="selectedId" name="file[0]" value="eCard" />eCard</li>
<li><input type="checkbox" class="selectedId" name="file[1]" value="eCommerce" />eCommerce</li>
<li><input type="checkbox" class="selectedId" name="file[2]" value="JATRx-Top-20" />JATRx-Top-20</li>
<li><input type="checkbox" class="selectedId" name="file[3]" value="MVSO" />MVSO</li>
<li><input type="checkbox" class="selectedId" name="file[4]" value="saris_web_design" />saris_web_design</li>
<li><input type="submit" name="mysubmit" value="Download!"></li>
</ul>
</form>
<?php
// common vars
$file_path = $_SERVER['DOCUMENT_ROOT']."/path/to/docs/";
if(count($_POST['file']) > 1){
//more than one file - zip together then download
$zipname = 'Forms-'.date(strtotime("now")).'.zip';
$zip = new ZipArchive();
if ($zip->open($zipname, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$zipname>\n");
}
foreach ($_POST['file'] as $key => $val) {
$files = $val . '.pdf';
$zip->addFile($file_path.$files,$files);
}
$zip->close();
//zip headers
if (headers_sent()) {
echo 'HTTP header already sent';
} else {
if (!is_file($zipname)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($zipname)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($zipname));
header("Content-Disposition: attachment; filename=\"".basename($zipname)."\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipname);
exit;
}
}
} elseif(count($_POST['file']) == 1) {
//only one file selected
foreach ($_POST['file'] as $key => $val) {
$singlename = $val . '.pdf';
}
$pdfname = $file_path. $singlename;
//header("Content-type:application/pdf");
header("Content-type: application/octet-stream");
header("Content-Disposition:inline;filename='".basename($pdfname)."'");
header('Content-Length: ' . filesize($pdfname));
header("Cache-control: private"); //use this to open files directly
readfile($pdfname);
} else {
echo 'no documents were selected. Please go back and select one or more documents';
}
?>
</code>
答案 1 :(得分:0)
错误有助于:
error_reporting(E_ALL);
ini_set('display_errors', '1');
尝试:
$zip->addFile($path, basename($path));
您可能还需要.zip
扩展程序:
$filename = "practiceforms.zip";
然后在发送正确的标题后,您需要将文件内容发送到浏览器:
readfile($filename);