PHP脚本以zip方式下载文件

时间:2013-11-19 18:30:56

标签: php zip ziparchive

我在我的服务器上设置一个脚本,该脚本使用用户发送的一些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 个答案:

答案 0 :(得分:-1)

这些链接可以帮助您:

  1. Using headers

  2. Creating zip file in PHP

相关问题