簡體   English   中英

PHP腳本以zip格式下載文件

[英]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文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM