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

Php 批量选择文件-压缩文件-然后下载到客户端,php,forms,download,zip,Php,Forms,Download,Zip,当我试图用我需要做的事情来寻找解决方案时,我似乎无法从我所发现的东西中得到任何东西 我正在尝试创建一个表单,用户可以在其中选择网页上列出的1个或多个(批量选择全部)pdf文件 当用户单击submit时,我想让我的PHP下载文件创建一个zip文件,将选定的pdf文件(存储在服务器上)添加到zip文件,然后强制将zip文件下载给最终用户 当用户打开zip文件时,他们从网页表单中选择的pdf文件将在那里供其使用 我发现的一切要么不起作用,要么不安全,要么与我不想先上传文件有关。这些文件已存储在我的服务

当我试图用我需要做的事情来寻找解决方案时,我似乎无法从我所发现的东西中得到任何东西

我正在尝试创建一个表单,用户可以在其中选择网页上列出的1个或多个(批量选择全部)pdf文件

当用户单击submit时,我想让我的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>
这些错误有助于:

error_reporting(E_ALL);
ini_set('display_errors', '1');
尝试:

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

$filename = "practiceforms.zip";
然后,在发送正确的标题后,您将需要以下内容将文件内容发送到浏览器:

readfile($filename);

这是我的最终工作版本-让我知道,如果你有任何关于精简它的建议。同时,非常感谢您的支持和投入

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

  • 埃卡德
  • 电子商务
  • JATRx-Top-20
  • MVSO
  • 纱丽网设计

您有什么东西吗?可能是表单?我的表单只是一个基本数组-eCard
<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>