Julian Lay
Julian Lay

Reputation: 11

My PHP Script dont want to create a ZIP File

I tried to code a script, that put some data in a zip file. I think I have done all right, but he does not create the zip file.

I already tried a lot, but don´t find the issue.

<?php
$sql_abfrage_cloud = "SELECT * FROM dateien WHERE code = '$zugang' ORDER BY id"; 
$abfrage_cloud = $mysqli->query($sql_abfrage_cloud);
$verzeichnis = '/upload/';
$zip_name = date("dHis").'_fc.zip';             
$anz_dateien = 0;
$error = 'fatal';
while($fetch = $abfrage_cloud->fetch_assoc()){
    $anz_dateien = $anz_dateien + 1;
    $zip_datei[$anz_dateien] = $fetch['path'];
}
$zip_arch = new ZipArchive;
$status = $zip_arch->open($zip_name, ZipArchive::CREATE);
if($status==true){
    foreach($zip_datei as $datei){
        $zip_arch->addFile($verzeichnis.$datei, $datei);
    }                                                                                      

    if(file_exists($zip_name)){
        $error = 'false';
    } else {
        $error = 'true';
    }
}
?>

I expected that $error would be 'false' but it´s 'true'.

Upvotes: 0

Views: 35

Answers (1)

Barmar
Barmar

Reputation: 781058

You need to call $zip_arch->close() to save finish writing the file.

You should also use === when comparing the result of $zip_archive->open(), since the non-true results are numbers, and any non-zero number compares equal to true when type juggling is allowed.

$zip_arch = new ZipArchive;
$status = $zip_arch->open($zip_name, ZipArchive::CREATE);
if($status===true){
    foreach($zip_datei as $datei){
        $zip_arch->addFile($verzeichnis.$datei, $datei);
    }                                                                                      
    $zip_arch->close();

    if(file_exists($zip_name)){
        $error = 'false';
    } else {
        $error = 'true';
    }
}

Upvotes: 1

Related Questions