PHP copy()将文件转换为文件夹

时间:2014-08-07 14:19:54

标签: php copy

我发现了Rich Rodecker(http://www.visible-form.com/blog/copy-directory-in-php/)的这段代码。除了文件夹中有文件之外,它还能正常工作,它会将它们转换为文件夹。

以下是上面链接中的PHP代码段。

function copyr($source, $dest){
    // Simple copy for a file
    if (is_file($source)) {
        $c = copy($source, $dest);
        chmod($dest, 0777);
        return $c;
    }
    // Make destination directory
    if (!is_dir($dest)) {
        $oldumask = umask(0);
        mkdir($dest, 0777);
        umask($oldumask);
    }
    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == "." || $entry == "..") {
            continue;
        }

        // Deep copy directories
        if ($dest !== "$source/$entry") {
            copyr("$source/$entry", "$dest/$entry");
        }
    }
    // Clean up
    $dir->close();
    return true;
}

copyr("copy","copy2");

例如,这是网站的当前结构

- Root
- - index.php (Code here that runs copy function)
- - copy (DIR)
- - - index.html (Dummy content in file)

当我运行index.php时,它会创建:

- Root
- - index.php (Code here that runs copy function)
- - copy (DIR)
- - - index.html (HTML)

- - copy2 (DIR)
- - - index.html (DIR)
- - - - EMPTY

任何人都可以看到问题所在并提供解决方案吗?我希望能够指定一个目录并让它备份整个目录,包括子文件夹和文件。

1 个答案:

答案 0 :(得分:0)

来自PHP manual

  

请注意,如果父目录没有,则is_file()返回false   + x为你设置;这是有道理的,但其他函数,如readdir()似乎没有这个限制。最终的结果是你可以   循环遍历目录的文件,但is_file()将始终失败。

检查源目录的权限。

相关问题