开发者

PHP zip function makes zip but downloads

开发者 https://www.devze.com 2023-02-10 20:13 出处:网络
I have a function, called zip, that makes a zip out of a folder. Works perfectly, except that it downloads the zip after making it. I want users to be able to download and make backups on demand. But

I have a function, called zip, that makes a zip out of a folder. Works perfectly, except that it downloads the zip after making it. I want users to be able to download and make backups on demand. But they are download when they are made as well. This is my PHP code:

    function zip($source, $destination){
    if (extension_loaded('zip') === true){
        if (file_exists($source) === true){
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE) === true ){
                $source = realpath($source);
                if (is_dir($source) === true){
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file){
                        $file = 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();
        }
    }
    return false;
}
zi开发者_如何学Cp("../data", "backup.zip");

Does anyone know how to fix my problem? Thanks in advance!

Note:

The plain-text version is: here


I had a similar problem but I was making the zip files differently so I dont know if this will apply. My issue was with Internet Explorer, I would create the zip file when the user clicked a link and in IE it would auto download when it was done but the other browsers would work fine.

I would check for the $_SERVER["HTTP_USER_AGENT"] in my link and I had to add "target=_blank" to it in order to stop IE from auto downloading.

0

精彩评论

暂无评论...
验证码 换一张
取 消