下载zip文件的PHP脚本


PHP Script to download files in zip

我正在服务器上设置一个脚本,该脚本使用用户发送的一些GET变量来决定在zip中包含哪些文件,然后将这些文件返回给用户。ZIP文件由脚本动态创建,名称始终为myZip.ZIP。

如果我可能有100个用户同时调用脚本,并且每个用户的zip中可能包含不同的文件,这会起作用吗?

谢谢!

脚本示例:

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

这些链接可能会帮助您:

  1. 使用标题

  2. 在PHP 中创建zip文件