簡體   English   中英

PHP Zip下載為空

[英]PHP Zip is downloaded empty

在我的網站上,用戶將能夠下載ZIP格式的多個文件。 問題是zip->close()刪除了我在foreach循環中添加的所有文件。

這是代碼:

  foreach ($file_name_list as $file_name) {
    var_dump($zip);
    echo '<br>';

    $zip->addFile($files_path . $file_name, $file_name);
  }

  var_dump($zip);
  echo '<br>';

  $zip->close();
  echo '<br>';
  echo 'After closing:<br>';

  var_dump($zip);
  echo '<br>';

輸出是:

object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" } 
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(1) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" } 
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(2) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" } 

Afterclosing:
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(0) "" ["comment"]=> string(0) "" } 

如您所見,當PHP運行zip->close()它將刪除添加的文件。 它還會刪除文件名。 我不知道出什么問題了,因為它可以在Windows本地計算機上正常運行,但不能在服務器上的Ubuntu虛擬機上運行。

如何解決?

我真的需要zip->close()嗎? 我只想下載文件,無論如何。

我自己找到了答案。

問題是,通過zip->open($zipname, ZipArchive::CREATE) zip時zip->open($zipname, ZipArchive::CREATE)必須使用zip的完整路徑,而不是zip名稱。 我認為另一個問題是該zip的創建權限很少,因此無法下載。

起作用的代碼:

function donwloadFilesInZip($array) { 
  $filesName = $array['files'];
  $filesPath = rtrim($array['filesDir'], '/');
  $zipName = $array['zipname'];
  $zipPath = "$filesPath/$zipName";

  $zip = new ZipArchive();
  if($zip->open($zipPath, ZipArchive::CREATE)) {
    foreach ($filesName as $fileName) {
      if(file_exists("$filesPath/$fileName")) {
        $zip->addFile("$filesPath/$fileName", $fileName);
      }
    }
    $zip->close();

    #file permissions
    chmod($zipPath, 777);
    #headers
    header('Content-type: application/zip'); 
    header("Content-Disposition: attachment; filename=$zipName");
    header('Content-length: ' . filesize($zipPath));

    #refresh the file permissions in order to read it
    ob_clean();
    flush();
    #download
    readfile($zipPath);
    #delete
    unlink($zipPath);
  } else {
    #display some error message here
  }
}

舊代碼(在我的Windows本地計算機上運行良好,但在Ubuntu中效果不佳):

function donwload_files_in_zip($array) {
    $file_name_list = $array['files_list'];
    $zip_name = $array['zipname'];
    $files_path = $array['files_path'];

    $zip = new ZipArchive();
    $zip->open($zip_name, ZipArchive::CREATE);
    foreach ($file_name_list as $file_name) {
      $zip->addFile($files_path . $file_name, $file_name);
    }

    $zip->close();

    // headers
    header('Content-Type: application/zip');
    header("Content-disposition: attachment; filename = $zip_name");
    header('Content-Length: ' . filesize($zip_name));

    // download
    readfile($zip_name);

    // delete
    unlink($zip_name);
}

暫無
暫無

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

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