Заархивированный файл с PHP приводит к файлу cpgz после извлечения

Я архивирую папки и файлы с помощью php, но когда я пытаюсь открыть zip-файл, вместо этого я получаю файл cpgz. После извлечения этого файла я получаю другой zip-файл. Что он делает, так это просто сканирует текущую папку на наличие файлов и папок для архивирования. Это код, который я использую:

function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
    return false;
}

$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
    return false;
}

$source = str_replace('\\', '/', realpath($source));

if (is_dir($source) === true)
{
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

    foreach ($files as $file)
    {
        $file = str_replace('\\', '/', realpath($file));

        if (is_dir($file) === true)
        {
            $zip->addEmptyDir(str_replace($source. '/', '', $file. '/'));
        }
        else if (is_file($file) === true)
        {
            $zip->addFromString(str_replace($source. '/', '', $file), file_get_contents($file));
        }
    }
}
else if (is_file($source) === true)
{
    $zip->addFromString(basename($source), file_get_contents($source));
}

return $zip->close();
}


if($_GET["archive"]== 'true'){
$date = date("Ymd_Hi");
$dir = dirname(__FILE__);
$filename = $date.".zip";

Zip(getcwd(), $filename);

header("Content-disposition: attachment; filename=$filename");
header('Content-type: application/zip');
readfile($filename);
unlink($filename);
}
5
задан Steven Dobbelaere 12 July 2012 в 14:33
поделиться