2009-10-03 86 views
4

我有一個名爲目錄「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

回答

0

你可能只是想上下移動該線,像這樣:

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的意見以瞭解其他可能性。

1
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 ) 「/測試」。);

這是我的建議。

0

我覺得taht的$ D->閱讀()也將返回父母的名字和帽子就是爲什麼你在目標目錄中再次創造它。

0

試試運行cp -a。這需要保留mod時間和權限,以及所有這些。 cp -al將建立一個硬鏈接場。

+1

「硬鏈接農場」聽起來很有趣! – 2010-01-28 07:53:33

1
<?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

2

你好,有小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

0

適用於Windows Server:

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

對於Linux服務器:

shell_exec('cp -R /old/folder /new/folder'); 
相關問題