-1

I'm trying to make a zip file download. So I try to make the code like this :

$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;
}

I think it was successful. But, when I tried to open the zip file, There is an error like this
An error occurred while loading the archive.



then I tried to make the code like this

$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);
}

but I got nothing. The same thing also happen when i change it to this :

$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);
}

is there anyone who can help me to solve this download zip file problem?

Sherly Febrianti
  • 1,097
  • 15
  • 33
  • You have question similar to this and you are still open that question here http://stackoverflow.com/questions/28206587/symfony-ziparchive-php-extension-class-not-found – Jun Rikson Jan 29 '15 at 05:29
  • umm,, no no.. that question is about the class not found.. But in this question, the zip is already okay.. But After `$zip->close()` i want to parse the zip as response to downloaded by the client.
    The first code (using binaryFileResponse) is okay, but when I try to open the zipped file, it cannot be open on the client side, whereas the zip is okay on the server.
    – Sherly Febrianti Jan 29 '15 at 06:34

1 Answers1

2

An error occurred while loading the archive. occured in your clients side is because :

  1. Your client doesn't have any application to open zip file.
  2. zip file corrupt or missing extension.
  3. There is possibility you never updated / fresh install. Try to update it sudo apt-get update
  4. Make sure your downloader app (like IDM or flareget) is working good. (I have problem with this, and when I disable the downloader app, it works) -By Asker

It is problem with client side, (connection or program error) or with the file it self. Try open the file using another PC.

Jun Rikson
  • 1,964
  • 1
  • 22
  • 43
  • Thanks for your answer! and I just edit your comment and adding one list, point number 4. My first code is working without any problem when I disable the flareget. – Sherly Febrianti Jan 29 '15 at 07:02