2016-09-28 65 views
-1

我需要能够做到这一点。 从带复选框的表单中选择多个图像或单个图像,然后通过PHP将其下载为Zip文件。 以这种方式,它允许用户选择他需要或不需要的图像。复选框形式:选择文件,通过PHP下载Zip和下载

这里是我的代码:

<form name="zips" action="download.php" method=POST> 
          <ul> 

           <li> 
            <input type="checkbox" class="chk" name="items[0]" id="img0" value="2015-Above-it-All"/> 
            <p>Above It All</p> 
           </li> 
           <li> 
            <input type="checkbox" class="chk" name="items[1]" id="img1" value="2015-Crocodile"/> 
            <p>Crocodile</p> 
           </li> 
           <li> 
            <input type="checkbox" class="chk" name="items[2]" id="img2" value="2015-Dandelion"/> 
            <p>Dandelion</p> 
           </li> 
           <li> 
            <input type="checkbox" class="chk" name="items[3]" id="img3" value="2015-Dearest-Sister"/> 
            <p>Dearest Sister</p> 

           </li> 

           <div style="text-align:center;" > 
           <input type="submit" id="submit" name="createzip" value="DOWNLOAD" class="subbutton-images" > 
          </div> 

         </form> 

然后PHP代码:

 <?php 
    // common vars 
    $file_path = $_SERVER['DOCUMENT_ROOT']."/img/press/"; 

    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['items'] as $key => $val) { 
$files = $val . '.jpg'; 
$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['items']) == 1) { 
//only one file selected 
foreach ($_POST['items'] as $key => $val) { 
$singlename = $val . '.jpg'; 
} 
$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'; 
} 
?> 

目前这个剧本让我从表单下载一个单一的形象,但只要有2个图像并且该脚本试图压缩文件,然后提供下载不再工作。

任何想法对你们来说都会很不错,因为我现在对scfript有点困惑吗?

+0

的[下载多个文件,在PHP的zip]可能的复制(http://stackoverflow.com/问题/ 1754352 /下载多个文件作为zip中的php) –

回答

1

所以下面是正确的PHP脚本为我工作: :))

<?php 
// common vars 
$file_path = $_SERVER['DOCUMENT_ROOT']."/img/press/"; 

if(count($_POST['items']) > 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['items'] as $key => $val) { 
$files = $val . '.jpg'; 
$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('Content-type: application/zip'); 
header('Content-Disposition: attachment; filename='.$zipname); 
header('Pragma: no-cache'); 
header('Expires: 0'); 
readfile($zipname); 
flush(); 
if (readfile($zipname)) 
{ 
    unlink($zipname); 
} 
//unlink($zipname); 

exit; 
} 
} 
} elseif(count($_POST['items']) == 1) { 
//only one file selected 
foreach ($_POST['items'] as $key => $val) { 
$singlename = $val . '.jpg'; 
} 
$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'; 
} 
?> 
0
 ***<?php 
// common vars 
$file_path = $_SERVER['DOCUMENT_ROOT']."/img/press/"; 
if(count($_POST['items']) > 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['items'] as $key => $val) { 
$files = $val . '.jpg'; 
$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('Content-type: application/zip'); 
header('Content-Disposition: attachment; filename='.$zipname); 
header('Pragma: no-cache'); 
header('Expires: 0'); 
readfile($zipname); 
flush(); 
if (readfile($zipname)) 
{ 
    unlink($zipname); 
} 
//unlink($zipname); 
exit; 
} 
} 
} elseif(count($_POST['items']) == 1) { 
//only one file selected 
foreach ($_POST['items'] as $key => $val) { 
$singlename = $val . '.jpg'; 
} 
$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 Document were selected .Please Go back and Select Document again'; 
} 
?>*** 
+1

虽然这段代码片段是受欢迎的,并可能提供一些帮助,它会[[如果包括一个解释大大改善](//meta.stackexchange .com/q/114762)*如何解决这个问题。没有这些,你的答案就没有什么教育价值了 - 记住,你正在为将来的读者回答这个问题,而不仅仅是现在问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 –