简体   繁体   中英

PHP Script to download files in zip

I'm setting up a script on my server that uses some GET variables sent by a user to decides which files to include in a zip that then gets returned to the user. The ZIP file is created dynamically by the script and will always have the name myZip.zip.

If I potentially have 100 users calling the script at the same time, and each user may have different files included in the zip, will this work?

Thanks!

example of script:

<?php

     $fileA = 'fileA.xml';
     $fileB = 'fileB.xml';
     $fileC = 'fileC.xml';

     $filesToSend = array();

     if(conditionA) {

        array_push($filesToSend, $fileA);
     }

    if(conditionB) {

        array_push($filesToSend, $fileB);
     }

     if(conditionC) {

        array_push($filesToSend, $fileC);
     }

     if(count($filesToSend) > 0) {

        $zipname = "myZip.zip";
        $zip = new ZipArchive();
        $res = $zip->open($zipname, ZipArchive::CREATE);

        if ($res === TRUE) {

            foreach ($filesToSend as $file) {

                $zip->addFile($file);
            }
        }
        else {

            echo 'failed, code:' . $res;
        }

        $zip->close();

        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header("Content-type: application/zip");
        header('Content-length: '.filesize($zipname));
        header('Content-disposition: attachment; filename=' . basename($zipname));
        ob_clean();
        flush();
        readfile($zipname);
        @unlink($file);
    }
    else {
        echo "nothing new";
        echo $modifiedDate;
    }
?>

These links may help you:

  1. Using headers

  2. Creating zip file in PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM