-1

I need to be able to do this. From a form with checkbox the user select multiple images or singles images and then download it via PHP as a Zip file. In that way it allows user to choose images he needs or not.

Here is my Code :

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

Then the PHP code:

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

At the moment this script let me download a single image from the form but as soon as there is 2 images and that the script try to ZIP the files and then offer download its not working anymore.

Any ideas would be pretty nice from you guys as i'm a bit stucked with the scfript at the moment?

  • Possible duplicate of [download multiple files as zip in php](http://stackoverflow.com/questions/1754352/download-multiple-files-as-zip-in-php) – Philipp Palmtag Sep 28 '16 at 08:02

2 Answers2

1

So below is the right PHP script which works for me : :))

 <?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
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight May 03 '17 at 13:13