复制所有文件和文件夹的从一个目录到另一个目录PHP

问题描述:

我有一个名为目录“mysourcedir”它有sonme文件和文件夹。所以我想将这个目录中的所有内容复制到使用PHP的Linux服务器上的其他“目标文件夹”。复制所有文件和文件夹的从一个目录到另一个目录PHP

function full_copy($source, $target) { 
if (is_dir($source)) { 
    @mkdir($target); 
    $d = dir($source); 
    while (FALSE !== ($entry = $d->read())) { 
     if ($entry == '.' || $entry == '..') { 
      continue; 
     } 
     $Entry = $source . '/' . $entry; 
     if (is_dir($Entry)) { 
      $this->full_copy($Entry, $target . '/' . $entry); 
      continue; 
     } 
     copy($Entry, $target . '/' . $entry); 
    } 

    $d->close(); 
}else { 
    copy($source, $target); 
} 
} 

我试着这个代码,但它有一些问题,它在目标位置创建目录“mysourcedir”。我期待只复制目的地的所有文件和文件夹。请建议

+0

嘿,看起来像我需要删除这条语句那是对的吗? @mkdir($ target); – santosh 2009-10-03 12:30:14

你可能只是想上下移动该线,像这样:

function full_copy($source, $target) { 
if (is_dir($source)) { 
     $d = dir($source); 
     while (FALSE !== ($entry = $d->read())) { 
       if ($entry == '.' || $entry == '..') { 
         continue; 
       } 
       $Entry = $source . '/' . $entry; 
       if (is_dir($Entry)) { 
         @mkdir($Entry); 
         $this->full_copy($Entry, $target . '/' . $entry); 
         continue; 
       } 
       copy($Entry, $target . '/' . $entry); 
     } 

     $d->close(); 
}else { 
     copy($source, $target); 
} 

虽然我个人将避免使用2个变量与仅captilisation相同的名称来区分它们。检查用户对http://www.php.net/copy的意见以了解其他可能性。

class FolderCopy { 


    public static function copyFolder($src, $dest) { 

    $path = realpath($src); 
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); 

     /** SplFileInfo $object*/ 
    foreach($objects as $name => $object) 
    { 
     $startsAt = substr(dirname($name), strlen($src)); 
     self::mkDir($dest.$startsAt); 
     if(is_writable($dest.$startsAt) and $object->isFile()) 
     { 
      copy((string)$name, $dest.$startsAt.DIRECTORY_SEPARATOR.basename($name)); 
     } 
    } 
    } 

    private static function mkDir($folder, $perm=0777) { 
    if(!is_dir($folder)) { 
     mkdir($folder, $perm); 
    } 
    } 

} 

FolderCopy :: copyFolder(目录名(目录名(FILE )) “/图像”,目录名(FILE ) “/测试”。);

这是我的建议。

我觉得taht的$ D->阅读()也将返回父母的名字和帽子就是为什么你在目标目录中再次创造它。

试试运行cp -a。这需要保留mod时间和权限,以及所有这些。 cp -al将建立一个硬链接场。

+1

“硬链接农场”听起来很有趣! – 2010-01-28 07:53:33

<?php 
/** 
* Copy a file, or recursively copy a folder and its contents 
* 
* @author  Aidan Lister <[email protected]> 
* @version  1.0.1 
* @param  string $source Source path 
* @param  string $dest  Destination path 
* @return  bool  Returns TRUE on success, FALSE on failure 
*/ 
function copyr($source, $dest) 
{ 
    // Simple copy for a file 
    if (is_file($source)) { 
     chmod($dest, 777); 
     return copy($source, $dest); 
    } 

    // Make destination directory 
    if (!is_dir($dest)) { 
     mkdir($dest); 
    } 

    chmod($dest, 777); 

    // 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; 
} 

?> 
+0

它是[即答案]的副本(http://php.net/manual/en/function.copy.php#91010)。 – 2015-07-16 16:22:11

+0

你为什么要chmod 777? – Seer 2015-10-22 20:42:42

你好,有小php开发人员这不是一个问题,但对如何将文件从一个文件夹复制到另一个问题的答案。我曾在互联网上看到一些开发人员使用rename()而不是copy()将文件从一个目录移动到另一个目录。 这里是简单的工作代码。测试和工作像魅力。

< ===========================魔术================ ============>

<?php  
    $dir = "path/to/targetFiles/"; 
    $dirNew="path/to/newFilesFolder/"; 
    // Open a known directory, and proceed to read its contents 
    if (is_dir($dir)) { 
     if ($dh = opendir($dir)) { 
      while (($file = readdir($dh)) !== false) { 
      //exclude unwanted 
      if ($file==".") continue; 
      if ($file=="..")continue; 
      //if ($file=="index.php") continue; for example if you have index.php in the folder 

      if (copy("$dir/$file","$dirNew/$file")) 
       { 
       echo "Files Copyed Successfully"; 
       //echo "<img src=$dirNew/$file />"; 
       //if files you are moving are images you can print it from 
       //new folder to be sure they are there 
       } 
       else {echo "File Not Copy";} 
      } 
      closedir($dh); 
     } 
    } 


    ?> 
+0

thanx很多...你的答案拯救我的一天 – deemi 2015-07-30 06:04:19

适用于Windows Server:

shell_exec('xcopy \\old\\folder \\new\\folder /s /e /y /i'); 

对于Linux服务器:

shell_exec('cp -R /old/folder /new/folder');