Pergunta

I can't seem to get anything working from what I am finding when I try to search for a solution with what I need to do.

I am trying to create a form where a user will select 1 or more (bulk select all also) pdf files listed on a webpage.

When the user click submit, I want to have my PHP download file Create a zip file, Add the selected pdf files (store on the server) to the zip file and then force download the zip file to the end user.

When the user opens the zip file, the pdf files they selected from the webpage form are there for their use.

Everything I find either does not work, is not secure or has something to do with uploading the files first which I don't want to do. The files are already stored on my server.

Any help will be much appreciated.

<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>
Foi útil?

Solução 2

The errors can help:

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

Try:

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

You might also want the .zip extension:

$filename = "practiceforms.zip";

Then after sending the proper headers you'll need this to send the file contents to the browser:

readfile($filename);

Outras dicas

Here is my final working version - Let me know if you have any suggestions on streamlining it. Also, Thank you very much for your support and input.

<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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top