-1

I am using PHP to download a number of files as zip folder, see code below, it shows me this error zipArchive::close() Read error: no such file or directory in C:// path

I have added HTML button on click to take the id of the row to download its files

if (isset($_GET['zip'])) {
$zip_id = $_GET['zip'];

  $query4 = "select f.id f.file_name, f.path, r.user_id from register r "
        . "join files f on f.register_id =r.user_id "
        . "where r.user_id=$zip_id";   
$result4 = mysqli_query($con, $query4);
if (($result4)) {
    while ($row = mysqli_fetch_array($result4)) {
        $document[$row['id']] = array(
            'id' => $row['id'],
            'file_name' => $row['file_name'],
            'path' => $row['path']
        );
    }
}
 // folder to load files  
if (extension_loaded('zip')) {
    // Checking ZIP extension is available  
    foreach($document as $f){
         $files[]='uploads/'.$f['path'];
    }
    if ($files != null) {

    // Checking files are selected  
        $zip = new ZipArchive(); // Load zip library   
        $zip_name = 'folder.zip';           // Zip name  
        if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
            // Opening zip file to load files  
            $error .= "* Sorry ZIP creation failed at this time";
        }
        foreach ($files as $file){
        $zip->addFile($file,$file);} // Adding files into zip  

        $zip->close();
        if (file_exists($zip_name)) {
            // push to download the zip  
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="' . $zip_name . '"');
            readfile($zip_name);

            // remove zip file is exists in temp path  
            unlink($zip_name);
        }
    } else {
        $error .= "*  no file to zip ";
    }
} else {

    $error .= "* You dont have ZIP extension";
}

}

Fatimah Mohmmed
  • 147
  • 2
  • 18
  • 2
    Your code is vulnerable to SQL injection. You should use prepared statements. Please read: https://stackoverflow.com/a/60496/1839439 – Dharman Sep 15 '19 at 08:32
  • @Dharman if I prepared my code as you request, will it solve my recent problem !! – Fatimah Mohmmed Sep 15 '19 at 08:58
  • It's not my request. I am warning you that your code contains a very serious bug. It might fix your issue, but it is likely a separate issue to what you are asking about. – Dharman Sep 15 '19 at 09:08

1 Answers1

0

I found the error in my code. I have declared & initialized the $document array inside the while loop which is not defined for the zipArchive(). So, it cannot see it then it returns null.

the correction I have defined the creation of the $files inside the while loop.

Fatimah Mohmmed
  • 147
  • 2
  • 18