user2460444
user2460444

Reputation: 33

PHP ZipArchive - Downloaded file says i'ts empty but it's not

I have the below code that creates a ZIP file, adds a file to it and then downloads to my computer.

$zip = new ZipArchive();
if ($zip->open('order_sheets.zip', ZipArchive::CREATE) === TRUE){
    $zip->addFile($pdfFilePath);
}
$zip->close();

$file_url = 'order_sheets.zip';
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$file_url);
header('Content-Length: ' . filesize($file_url));
readfile($file_url);

All works good but the issue is, when opening the downloaded ZIP, it say's This folder is empty when it is not. If i right click and hit "Extract Here", the contents come out.

Anyone know why that is?

Upvotes: 0

Views: 462

Answers (1)

Headbank
Headbank

Reputation: 400

The problem is with Windows' zip utility, which uses IBM850 encoding causing it to misinterpret some characters in internal filenames in the archive, including _ (underscore) as in your file.

See answer here: PHP ZipArchive Corrupt in Windows

Explained in PHP Manual user notes here: http://php.net/manual/en/ziparchive.addfile.php#95725

Upvotes: 1

Related Questions