2016-09-28 7 views
-1

이 작업을 수행 할 수 있어야합니다. 체크 박스가있는 양식에서 사용자가 여러 개의 이미지를 선택하거나 이미지를 선택하고 PHP를 통해 Zip 파일로 다운로드합니다. 이렇게하면 사용자가 필요로하는 이미지를 선택할 수 있습니다. 여기 확인란 양식 : 파일, 우편 번호 및 PHP를 통해 다운로드

내 코드입니다 :

<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'; 
} 
?> 

그리고 스크립트가 파일을 ZIP하려고 시도하고 더 이상 작동하지 않는 다운로드를 제공한다고.

혹시 내가 지금 scfript에 갇혀있는 아이디어가 있으면 좋겠다. 그래서 아래

+0

가능한 중복 (http://stackoverflow.com/ 질문/1754352/download-multiple-files-as-zip-in-php) –

답변

1

은 나를 위해 작동 권리 PHP 스크립트입니다 : :)) 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). 그것이 없으면 당신의 대답은 교육 가치가 훨씬 낮아집니다 - 당신이 지금 묻는 사람이 아니라 미래의 독자들에게 질문에 답하고 있다는 것을 기억하십시오! 설명을 추가하려면 답을 편집하고 어떤 제한 및 가정이 적용되는지 표시하십시오. –

관련 문제