簡體   English   中英

ZIP文件將無法下載,並且與php行為怪異

[英]ZIP file won't download and acts weird with php

我正在laravel中的一個系統上制作一個充滿照片的zip文件,然后下載。

我決定我真的不想為此使用庫(這是必要的),所以我必須使用純PHP。 我的控制器代碼:

public function downloadPictures()
{
    $pictures = Input::get('photos');
    $file_paths = array();

    foreach ($pictures as $picture_id) {
         $path = $this->photos->getFilepath($picture_id, 'original');
         array_push($file_paths, $path);
    }

    $zip = new ZipArchive;
    $zip->open('slike.zip', ZipArchive::CREATE);

    foreach ($file_paths as $file) {
        $content = file_get_contents($file);
        $zip->addFromString(basename($file), $content);
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($zip->filename).'"');

    echo var_dump($zip);
    echo basename($zip->filename);

    $zip->close();

    echo var_dump($zip);
    echo basename($zip->filename);
}

現在,使用代碼的第一部分,我可以100%確信地說我得到了正確的圖片路徑(即在//if (file_exists($file_paths ... )注釋上方,並且我通過將它們打印出來並使用file_exists

但是隨后事情開始變得不靈通。

首先:當我打開zip時,我測試的numFiles說里面已經有4個文件了,我不知道為什么。

其次:當我在js前端打印此控制器函數的響應時(在控制器var_dump($zip)使用echo),我獲得了zip文件的屬性。name + numFile + 4(由於某些原因,為+4),但是當我$zip->close()我無法訪問屬性,回顯的zip屬性為空。

第三:重定向標頭,在兩種情況下我都使用過:在我關閉$ zip之前和之后(當前它們在關閉之前)沒有任何作用..它們應該在瀏覽器中生成下載形式,不是嗎?

如果有人可以幫助我,我將非常感激。 我需要在周日做這個,我已經擺弄了大約8個小時。(這是我第一次這樣做)。 我已經做了很多谷歌搜索,它對其他人也有用。 我在nginx,php v5.6上的ununtu上安裝了php zip擴展名,並且正在ubuntu mozilla瀏覽器上對其進行本地測試。

更新:它也不適用於chrome,所以這不是Firefox的問題。

嘗試這個:

public function downloadPictures()
{
    $pictures = Input::get('photos');
    $file_paths = array();

    foreach ($pictures as $picture_id) {
        $path = $this->photos->getFilepath($picture_id, 'original');
        array_push($file_paths, $path);
    }

    $zip = new ZipArchive;
    $zipname = 'silke.zip';
    $zip->open($zipname, ZipArchive::CREATE);

    foreach ($file_paths as $file) {
        $content = file_get_contents($file);
        $zip->addFromString(basename($file), $content);
    }

    header("HTTP/1.1 200 OK");
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zipname.'"');
    header('Content-Length: ' . filesize($zipname));

    $zip->close();
    readfile($zipname);
}

最重要的是Content-Lengthreadfile行。

不要回顯其他任何內容,它會破壞下載。

更新:更加獨立的測試:

$testData = array(
    'test1.txt' => 'Test1',
    'test2.txt' => 'Test2',
    'test3.txt' => 'Test3',
    'test4.txt' => 'Test4',
);

$zip = new ZipArchive;
$zipname = 'slike.zip';
$zip->open($zipname, ZipArchive::CREATE);

foreach ($testData as $filename => $content) {
    $zip->addFromString($filename, $content);
}

header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.$zipname.'"');
header('Content-Length: ' . filesize($zipname));

$zip->close();
readfile($zipname);

它還具有header('Content-Type: application/force-download');

暫無
暫無

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

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