简体   繁体   English

PHP脚本以zip格式下载文件

[英]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. 我正在服务器上设置脚本,该脚本使用用户发送的一些GET变量来确定要包含在zip中的文件,然后将其返回给用户。 The ZIP file is created dynamically by the script and will always have the name myZip.zip. ZIP文件是由脚本动态创建的,并且始终具有名称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? 如果我可能有100个用户同时调用该脚本,并且每个用户的zip文件中可能包含不同的文件,那么这行得通吗?

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 在PHP中创建zip文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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