jacek wrote:That is probably caused by the ziping stuff causing a php error which makes the file invalid. comment out everything after $zip->close() and reload the page. That should show you the error instead of including it at the start of the file.
I further modified the script:
<?php
$dir = 'sample';
$archive = 'sample.zip';
function create_zip($folder, &$zipFile, $subfolder = null) {
$folder .= end(str_split($folder)) == "/" ? "" : "/";
$subfolder .= end(str_split($subfolder)) == "/" ? "" : "/";
echo 'Folder: '.$folder;
echo '<br />';
echo 'Subfolder: '.$subfolder;
$handle = opendir($folder);
while ($file = readdir($handle)) {
if ($file != '.' && $file != '..') {
if (is_file($folder.$file)) {
if ($subfolder != null)
$zipFile->addFile($folder.$file, $subfolder.$file);
else
$zipFile->addFile($folder.$file);
} elseif (is_dir($folder.$file)) {
$zipFile->addEmptyDir($file);
create_zip($folder.$file, $zipFile, $file);
}
}
}
}
$zip = new ZipArchive;
$zip->open($archive, ZIPARCHIVE::CREATE);
create_zip($dir, $zip);
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);
?>
It still gives the same error when I try to open the zip file. I tried your suggestion and here is the error:
"Strict Standards: Only variables should be passed by reference"
for the following lines:
$folder .= end(str_split($folder)) == "/" ? "" : "/";
$subfolder .= end(str_split($subfolder)) == "/" ? "" : "/";