2016-08-24 304 views
0

我想通过我的服务器开始下载一个zip文件。为什么我的.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(); 
} 
+0

基本调试:'readfile($ files);'当你传入你的directoryiterator对象时,你认为readfile()会做什么? –

+0

我想我的.zip文件被下载到我的电脑用户 – David

+0

那么你为什么要提供一个对象到一个函数,需要一个文件名? –

回答

0

移动$zip->close();代码来readfile($filename)之前。