doulanli6146 2014-10-24 19:50
浏览 13

too long

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")
?>
  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2014-10-24 20:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集