批量选择文件-压缩文件-然后下载到客户端


Bulk Select files - Zip Files - Then Download to client

当我试图用我需要做的事情搜索解决方案时,我似乎无法从我的发现中得到任何工作。

我正在尝试创建一个表单,用户可以选择网页上列出的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>

这是我的最终工作版本-如果你对精简它有任何建议,请告诉我。此外,非常感谢你的支持和投入。

<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>

错误可以帮助:

error_reporting(E_ALL);
ini_set('display_errors', '1');

尝试:

$zip->addFile($path, basename($path));

您可能还需要.zip扩展名:

$filename = "practiceforms.zip";

然后,在发送正确的标题后,您将需要这个来将文件内容发送到浏览器:

readfile($filename);