Вопрос

It seems I successfully create an on-the-fly zip archive (filesize and file_exists both return the expected values) but when I attempt to actually download it, I receive an empty ZIP file. Curiously enough this error occurs with both readfile and fread. This is my code

$filename = $zip;
    $handle = fopen($filename, 'r');
    if ($handle === false)
    {
        die('Could not read file "' . $filename . '"');
    }

    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="fsdownload.zip"');
    header('Cache-Control: private');
    while (!feof($handle))
    {
        echo fread($handle, 8192);
    }
    fclose($handle);

This works fine for zip-Files < 10 MB. Any thoughts on what the problem might be?

Это было полезно?

Решение

To avoid consuming too much memory, you can use ZipStream or PHPZip, which will send zipped files on the fly to the browser, divided in chunks, instead of loading the entire content in PHP and then sending the zip file.

Both libraries are nice and useful pieces of code. A few details:

  • ZipStream "works" only with memory, but cannot be easily ported to PHP 4 if necessary (uses hash_file())
  • PHPZip writes temporary files on disk (consumes as much disk space as the biggest file to add in the zip), but can be easily adapted for PHP 4 if necessary.

Related SO questions:

Другие советы

The problem is a PHP limit. Check for memory and execution time limits.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top