简体   繁体   中英

Php function zip folder no works

I use one function I see here for zip folders , and works nice but I see great problem when I use this kind of slash ../

The function :

<?php
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('\\', '/', $file);

            // Ignore "." and ".." folders
            if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
                continue;

            $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();
}
?>

In my case i need zip folders out of where file with function for zipping works

When i use this function with this slash , the result it´s inside zip there are nothing , it´s empty

If i try use this code with folders in the same level , and no need use "../" all works fine

Actually my problem it´s when use the function in this way :

<?php
    Zip("../images","backup_images.zip")
?>

But if i use as here , works right

<?php
    Zip("images","backup_images.zip")
?>

Can you substitute the absolute path instead?

Zip("../images","backup_images.zip")

becomes:

Zip("/var/www/vhosts/domain.com/httpdocs/full/path/to/images","backup_images.zip")

You can use phpinfo(); within a PHP file in the same place as backup_images.zip to find the correct directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM