簡體   English   中英

創建zip文件並下載

[英]Create a zip file and download it

我試圖通過在本地服務器上創建 zip 文件來下載 2 個文件。該文件以 zip 格式下載,但是當我嘗試提取它時。它給出錯誤:未找到中央目錄簽名結束。 該文件不是 zip 文件,或者它構成了多部分存檔的一個磁盤。 在后一種情況下,中央目錄和 zip 文件注釋將在此存檔的最后一張磁盤上找到。

我正在使用以下代碼:

 <?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');

//Archive name
$archive_file_name=$name.'iMUST_Products.zip';

//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';


zipFilesAndDownload($file_names,$archive_file_name,$file_path);

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
        //echo $file_path;die;
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files,$files);
        //echo $file_path.$files,$files."

    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}




?>

我檢查了傳遞給 function 的所有變量的值,一切都很好。所以請看這個。提前致謝。

添加描述zip文件大小的Content-length標頭,以字節為單位。

header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$archive_file_name");

還要確保<?之前絕對沒有空白區域<? 之后?> 我在這里看到一個空格:

 <?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);

我在這里找到了這個解決方案,它對我有用

$zip = new ZipArchive;
$tmp_file = 'assets/myzip.zip';
    if ($zip->open($tmp_file,  ZipArchive::CREATE)) {
        $zip->addFile('folder/bootstrap.js', 'bootstrap.js');
        $zip->addFile('folder/bootstrap.min.js', 'bootstrap.min.js');
        $zip->close();
        echo 'Archive created!';
        header('Content-disposition: attachment; filename=files.zip');
        header('Content-type: application/zip');
        readfile($tmp_file);
   } else {
       echo 'Failed!';
   }

我剛遇到這個問題。 對我來說問題是:

readfile("$archive_file_name");

這導致內存不足錯誤。

Allowed memory size of 134217728 bytes exhausted (tried to allocate 292982784 bytes)

我能夠通過使用以下內容替換readfile()來糾正問題:

    $handle = fopen($zipPath, "rb");
    while (!feof($handle)){
        echo fread($handle, 8192);
    }
    fclose($handle);

不確定這是否是同一個問題或者沒有看到您的文件只有1.2 MB。 也許這會幫助其他有類似問題的人。

其中一個錯誤可能是文件未被讀取為“存檔”格式。 檢查ZipArchive沒有打開文件 - 錯誤代碼:19 在文本編輯器中打開下載的文件,如果在啟動時有任何html標簽或調試語句,請在讀取文件之前清除緩沖區。

ob_clean();
flush();
readfile("$archive_file_name");

我遇到了完全相同的問題。 就我而言,它的來源是我想要創建zip文件的文件夾的權限,這些文件都設置為只讀。 我把它改成了讀寫,然后才有效。

如果在運行腳本時未在本地服務器上創建該文件,則很可能遇到與我相同的問題。

但是我在下載后從服務器獲取的文件給出了226字節的大小

這是ZIP標題的大小。 顯然,下載的ZIP文件中沒有數據 那么,您是否可以驗證要添加到ZIP文件中的文件確實存在(相對於下載PHP腳本的路徑)?

考慮添加對addFile的檢查:

foreach($file_names as $file)
{
    $inputFile = $file_path . $file;
    if (!file_exists($inputFile))
        trigger_error("The input file $inputFile does not exist", E_USER_ERROR);
    if (!is_readable($inputFile))
        trigger_error("The input file $inputFile exists, but has wrong permissions or ownership", E_USER_ERROR);
    if (!$zip->addFile($inputFile, $file))
        trigger_error("Could not add $inputFile to ZIP file", E_USER_ERROR);
}

觀察到的行為與某些問題(路徑錯誤,權限問題,......)一致,導致文件無法添加到ZIP文件中。 在收到“空”ZIP文件時,客戶端發出一個錯誤,指的是缺少ZIP中心目錄(實際錯誤是沒有目錄,沒有文件)。

確保$archive_file_name中的路徑正確且確實存在。 在您的代碼中,您通過放置一個未聲明的前綴變量 - $name更新了$archive_file_name變量。

$archive_file_name = $name.'iMUST_Products.zip';

如果路徑不正確,找不到,則創建的壓縮文件無處可去 go ,這兩個文件無處可存。 要存儲的文件的路徑也應該存在。

假設您只想自定義要下載的壓縮文件夾的名稱,這就是為什么有一個$name變量來防止冗余文件名。 但是您必須考慮運行腳本的 URI。

例如,您正在使用 URI 路由,並在https://sample.co/file-management/download-zipped-111加載腳本。 使用您當前的腳本,壓縮文件夾將存儲在file-management文件夾中。 如果它不存在,您將主要遇到您現在遇到的錯誤。

解決方案是顯式聲明將存儲壓縮文件的文件夾的路徑。

例如,您想將壓縮文件存儲在Harshal/files/zipped/ 通過在files夾中創建一個名為zipped的文件夾來確保此文件夾存在,並且您有權寫入此文件夾。

$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}

人們可能會建議 - “為什么不直接將完整路徑放入$archive_file_name變量?

$archive_file_name = $file_path.'zipped/'.$name.'iMUST_Products.zip';

如果我們直接將其放入$archive_file_name變量,將要下載的文件很可能會命名為/var/www/Harshal/files/zipped/xxx-iMUST_Products.zip

暫無
暫無

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

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