为什么我的.zip文件被空载下载?

时间:2016-08-24 16:30:12

标签: php html5 ziparchive

我正在尝试通过我的服务器下载zip。

此类型的zip文件包含许多生成的pdf。

问题是袋子和倾倒它但是标记我打开这个损坏或有一个错误,当我提取告诉我是空的。

我在这里完成整个程序?

descargar.php代码

<?php

$zip = new ZipArchive();

$filename = 'walkingdead.zip';


if($zip->open($filename,ZIPARCHIVE::CREATE)===true) {
// Get real path for our folder
$rootPath = realpath('../walkingdead');


$zip->open('walking.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

 //Sin notificaciones, y que el server no comprima
@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
@ini_set('zlib.output_compression', 'Off');
  //Encabezados para archivos .zip
header('Content-Type: application/zip');
header('Content-Transfer-Encoding: Binary');
  //El nombre predeterminado que verá el cliente
header('Content-disposition: attachment; filename="' . basename($filename) . '"');
  //Que no haya límite en la ejecución del script
@set_time_limit(0);

  //Imprime el contenido del archivo
readfile($filename);


// Zip archive will be created only after closing object
$zip->close();
}

1 个答案:

答案 0 :(得分:0)

$zip->close();移至readfile($filename)之前。