【问题标题】:PHP: Recursive copyPHP:递归复制
【发布时间】:2011-05-27 02:09:23
【问题描述】:

我正在使用此代码递归复制目录(和文件)。我不明白为什么,但是,复制后,我的 $source 文件夹增加了......它有 76.3MB,复制后它会增加到 123 MB!有什么想法吗?

<?php

class MyDirectory {

    public function copy($source, $destination, $directoryPermission = 0755, $filePermission = 0644) {
        $source = $this->addSlash($source);
        $destination = $this->addSlash($destination);
        $directoryIterator = new DirectoryIterator($source);

        if (!file_exists($destination)) {
            mkdir($destination, $directoryPermission);
        }

        foreach ($directoryIterator as $fileInfo) {
            $filePath = $fileInfo->getPathname();
            $newDestination = str_replace($source, $destination, $filePath);

            if (!$fileInfo->isDot()) {
                if ($fileInfo->isFile()) {
                    copy($filePath, $newDestination);
                    chmod($newDestination, $filePermission);
                } else if ($fileInfo->isDir()) {
                    mkdir($newDestination, $directoryPermission);
                    $this->copy($filePath, $newDestination);
                }
            }
        }
    }

    private function addSlash($directory) {
        if (!empty($directory)) {
            if (!preg_match('/\/$/', $directory)) {
                $directory .= '/';
            }

            return $directory;
        }
    }

}

更新:增加源大小没有显着差异!

$ diff -rq mag/ copy-mag/
Only in mag//app/etc: local.xml

谢谢。

【问题讨论】:

  • 无意冒犯,但:这是目的地,不是命运:)
  • 我想补充一点,用 str_replace 创建来推断目标目录是个坏主意。如果路径的一部分重复,你就完蛋了。
  • Evert,还有其他选择吗?谢谢。
  • 你知道还有一个 RecursiveDirectorIterator 吗?

标签: php recursion copy directory


【解决方案1】:

很难从源代码中推断出哪里出了问题。

改为分析目录,比较差异,然后用结果更新您的问题。您可以使用Beyond Compare 之类的工具(商业但提供 30 天试用)来找出问题所在。

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 2013-11-06
    • 1970-01-01
    • 2015-08-19
    • 2013-10-19
    • 1970-01-01
    • 2011-02-27
    • 2016-07-28
    • 2011-11-21
    相关资源
    最近更新 更多