简体   繁体   中英

Why my .zip file is downloaded empty?

I'm trying to start a download of a zip through my server.

This type of zip file contains a number of pdf that are generated.

The problem is that the bags and dump it but mark me open this damaged or there is an error and when I extract tells me is empty.

Here I do the whole procedure?

Code of 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();
}

Move $zip->close(); to before readfile($filename) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM