簡體   English   中英

Symfony無法打開下載的zip文件

[英]Symfony cannot open the downloaded zip file

我正在嘗試下載一個zip文件。 所以我嘗試使代碼如下:

$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
    $basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
    foreach ($listToken as $token) {
        $file = $repoMarks->findByToken($token);
        if($file) {
            $fileName = $file[0]->getNameOnServer();
            $filePath = $basePath . $fileName;
            $root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
            $filePath = $root . '/' . $fileName;
            if (file_exists($filePath)) {
                $zip->addFile($filePath, $fileName);
            }
        }
    }
    $zip->close();

     $root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
     $zipFilePath = $root . '/' . $zipName;
     // prepare BinaryFileResponse
     $response = new BinaryFileResponse($zipFilePath);
     $response->trustXSendfileTypeHeader();
     $response->headers->set('Cache-Control', 'public');
     $response->headers->set('Content-type', 'application/zip');
     $response->setContentDisposition(
         ResponseHeaderBag::DISPOSITION_INLINE,
         $zipName,
         iconv('UTF-8', 'ASCII//TRANSLIT', $zipName)
     );
     return $response;
}

我認為這是成功的。 但是,當我嘗試打開zip文件時,出現了這樣的錯誤
An error occurred while loading the archive.



然后我試圖使這樣的代碼

$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
    $basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
    foreach ($listToken as $token) {
        $file = $repoMarks->findByToken($token);
        if($file) {
            $fileName = $file[0]->getNameOnServer();
            $filePath = $basePath . $fileName;
            $root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
            $filePath = $root . '/' . $fileName;
            if (file_exists($filePath)) {
                $zip->addFile($filePath, $fileName);
            }
        }
    }
    $zip->close();
    header('Content-Type', 'application/zip');
    header('Content-disposition: attachment; filename="' . $zipName . '"');
    header('Content-Length: ' . filesize($zipName));
    readfile($zipName);
}

但是我什么都沒有。 當我將其更改為此時,也會發生相同的事情:

$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
    $basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
    foreach ($listToken as $token) {
        $file = $repoMarks->findByToken($token);
        if($file) {
            $fileName = $file[0]->getNameOnServer();
            $filePath = $basePath . $fileName;
            $root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
            $filePath = $root . '/' . $fileName;
            if (file_exists($filePath)) {
                $zip->addFile($filePath, $fileName);
            }
        }
    }
    $zip->close();
    header("HTTP/1.1 303"); // 303 is technically correct for this type of redirect
    header("Location: http://{$_SERVER['HTTP_HOST']}/" . $fileName);
}

有誰可以幫助我解決此下載zip文件問題?

An error occurred while loading the archive. 在您的客戶端發生的原因是:

  1. 您的客戶端沒有任何應用程序可打開zip文件。
  2. zip文件損壞或擴展名丟失。
  3. 有可能您從未更新過/全新安裝。 嘗試更新它sudo apt-get update
  4. 確保您的下載器應用程序(例如IDM或flareget)運行良好。 (我對此有疑問,並且當我禁用下載器應用程序時,它可以工作) -由Asker

客戶端(連接或程序錯誤)或其自身文件有問題。 嘗試使用另一台PC打開文件。

暫無
暫無

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

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