PHP created a ZIP file that does not work with Windows Explorer

I have the following code that works fine on Windows with WinRAR and works fine on Mac. However, for some reason, when you open it using Windows Explorer by default, the zip code looks empty, and when you right-click and retrieve it, it says it's invalid. When the same one opens with winrar or on mac, all 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");
+4
source share
3 answers

: zip uploads ../dashboard/uploads/,

0

, .

foreach (glob("../dashboard/uploads/".$passcode."/*.jpg") as $file) { 
    $zip->addFile(realpath($file), pathinfo($file, PATHINFO_BASENAME));
}

, .

+1

only my $ 0.02 - the Windows file manager does not like it if the files in the ZIP archives are stored with a leading delimiter.

This does not work:

$zip->addFile(realpath($file), '/mydir/myfile.txt');

it works:

$zip->addFile(realpath($file), 'mydir/myfile.txt');
0
source

Source: https://habr.com/ru/post/1530122/


All Articles