PHP - Archivo zip bajado con tamaño 0 o vacio./volume1/web/familia/fotos/album/rodriguezobject(ZipArchive)

 
Vista:
Imágen de perfil de jose
Val: 184
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Archivo zip bajado con tamaño 0 o vacio./volume1/web/familia/fotos/album/rodriguezobject(ZipArchive)

Publicado por jose (71 intervenciones) el 12/03/2021 15:32:39
Buenas tardes,

Tengo una función de php para crear un archivo zip y bajarlo a través del navegador.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function comprimir($dir) {
 
  // Get real path for our folder
$rootPath = realpath($dir);
 
// Initialize archive object
$zip = new ZipArchive();
$filename = "../../fotos/zip/Fotos.zip";
$zip->open($filename, 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);
 
    }
 
}
var_dump($zip);
// Zip archive will be created only after closing object
$zip->close();
 
 
$fileName = basename($filename);
//$filePath = 'files/'.$fileName;
if(!empty($filename) && file_exists($filename)){
    // Define headers
  header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$fileName");
	header('Content-Length: ' . filesize($fileName));
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
 
 
    // Read the file
    readfile($filename);
    exit;
}else{
    echo 'The file does not exist.';
}
//Limpiando archivo
 //unlink($filename);
}

El resultado que me da var_dump($zip) es: code]/volume1/web/familia/fotos/album/rodriguezobject(ZipArchive)#1 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(192) ["filename"]=> string(40) "/volume1/web/familia/fotos/zip/Fotos.zip" ["comment"]=> string(0) "" }[/code]

Y el archivo lo guarda correctemante, ocupa el tamaño y contiene los archivos correctos.

Sin embargo baja con 0 bytes.

He realizado varios cambios con los headers y no encuentro la forma, podría alguién decirme donde está el problema.

Muchas gracias,
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de jose
Val: 184
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Archivo zip bajado con tamaño 0 o vacio./volume1/web/familia/fotos/album/rodriguezobject(ZipArchive)

Publicado por jose (71 intervenciones) el 12/03/2021 16:15:47
Tras hacer un echo readfile($filename); me daba es output:

1
Allowed memory size of 134217728 bytes exhausted

Por lo que subí la memoria a 256 Mb

Ahora baja el archivo correctamente con el tamaño que corresponde, pero al abrirlo dice que no es un archivo zip valido.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar