开发者

Zip function not working properly on server

Any ideas on why this is perfectly working in my localhost but not in the server where I uploaded it to? In the server, it creates the zip but does not create the folders, it puts all the files inside the .zip, with no folders distinction.

function rzip($source, $destination) {
    // create object
    $zip = new ZipArchive();
    // open archive
    if ($zip->open($destination, ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }

    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source));
    foreach ($iterator as $key=>$value) {
        $new_filename = substr($key,strrpos($key,"/") + 1);
        $zip->addFile(realpath($key), $new_filename) or die ("ERROR: Could not add file: $key");
    }

    $zip-开发者_Python百科>close();
}


You are mis-using (or not using) the RecursiveDirectoryIterator in places.

The first point is that you will iterate over the dot folders (. and ..) which is probably undesired; to stop this, use the SKIP_DOTS flag.

Next, there are tools to get the file's path relative to the main directory being iterated over and to get the real path too; using the getSubPathname() and getRealpath() methods, respectively.

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
                $source, RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($iterator as $key => $value) {
    $localname = $iterator->getSubPathname();
    $filename  = $value->getRealpath();
    $zip->addFile($filename, $localname) or die ("ERROR: Could not add file: $key");
}

The above is only an answer because it's too long for a comment. Nothing above answers why, "this is perfectly working in my localhost but not in the server".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜