簡體   English   中英

使用php多次下載文件

[英]Multiple download file using php

我正在建立一個站點,允許用戶根據用戶選擇下載一組圖像。 如何下載多個文件? 一次可以下載多個文件嗎?

如果一次只能下載1個文件,我如何“創建” 5個文件,將其壓縮,然后讓用戶下載該產品zip文件?

謝謝

一次只能下載一個。

如果您想壓縮文件,請查看

您可以使用file_put_contents ()創建文件。

更新資料

再次考慮...如果為每個文件打開一個新的選項卡/窗口,也許您可​​以啟動多個下載。 但是瀏覽器可能會阻止這些彈出窗口:

<script type="text/javascript">
    window.open('download.php?name=file1','_newtab');
    window.open('download.php?name=file2','_newtab');
    window.open('download.php?name=file3','_newtab');
</script>

您可以下載一系列壓縮文件格式,如下所示:

http://www.php.net/manual/zh/refs.compression.php

您無法下載多個文件。 您必須將它們壓縮並在zip上重定向用戶的瀏覽器。

您可以使用PHP ZIP類來滿足您的需求。

此代碼為您創建一個zip存檔:

    <?php
function createZipFile($files = array(), $filePath){
    // Check if $files we had contains files
    if(is_array($files)){
        // $files contains files, loop through them
        foreach($files as $file){
            // Does the file exist?
            if(file_exists($file)){
                $filesToBeZipped[] = $file;
            }
        }
    }

    if(count($filesToBeZipped)){
        // Create your new zip archive
        $zipArchive = new ZipArchive();
        if($zipArchive->open($filePath, ZIPARCHIVE::CREATE) !== true){
            return false;
        }else{
            foreach($filesToBeZipped as $file) {
                $zipArchive->addFile($file,$file);
            }

            $zipArchive->close();
            return file_exists($filePath);
        }
    }else{
        return false;
    }
}

$files = array(
  'filename1.extension',
  '/path/to/filename/filename.extension',
);

$action = createZipFile($files,'yourSampleArchive.zip');
?>

PHP一次只能發送一個文件,但是您可以使用php生成javascript代碼,從而調用多個php文件。

暫無
暫無

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

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