【问题标题】:Bulk Select files - Zip Files - Then Download to client批量选择文件 - 压缩文件 - 然后下载到客户端
【发布时间】:2014-04-22 17:20:40
【问题描述】:

当我尝试寻找我需要做的解决方案时,我似乎无法从我的发现中得到任何工作。

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

【问题讨论】:

  • 你有什么吗?也许是一种形式?
  • 我的表单只是一个基本数组 - eCard

标签: php forms download zip


【解决方案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>

【讨论】:

    【解决方案2】:

    这些错误可以提供帮助:

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

    试试:

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

    您可能还需要.zip 扩展:

    $filename = "practiceforms.zip";
    

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

    readfile($filename);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2014-08-05
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多