Search code examples
phpzip

PHP created ZIP file not working with Windows Explorer


I have the following code which works fine on windows with WinRAR and works fine on Mac's as well. However, for some reason, when you open it with the default windows Explorer, the zip appears empty and when you right click and extract, it says it is invalid. When the same one is opened with winrar or on a mac, all the files are there. Any ideas?

$passcode = $_GET['passcode'];

    $zip = new ZipArchive;
    $download = 'download_files_'.$passcode.'.zip';
    $zip->open($download, ZipArchive::CREATE);
    foreach (glob("../dashboard/uploads/".$passcode."/*.jpg") as $file) { /* Add appropriate path to read content of zip */
        $zip->addFile($file);
    }
    $zip->close();
    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename = $download");
    header('Content-Length: ' . filesize($download));
    header("Location: $download");

Solution

  • This was easily fixed by using the following method: I had to move the generate zip file to inside the uploads folder and remove ../dashboard/uploads/ as it was this path that was causing windows to think of it as a corrupt file