2014-11-22 169 views
2

是否有可能/容易將文件從一個位置(比如說在index.php/mypage/homes/)文件夾在另一個文件夾複製(例如,在friends可能有pfoxsamuni)使用PHP? 那麼文件index.php會被複制到friends/pfox,friends/uni等等?將一個文件複製到文件夾中的所有文件夾?


是顯而易見的,文件結構是這樣:

update.php (The file you have to write) 
mypage 
|_homes 
     |_index.php 
friends 
|_pfox <--Copy index.php to here! 
|_sam <--And here! 
|_uni <--And here! 
+0

如果存在'/ friends/one/sub'目錄會怎麼樣?你需要把它放到每個子文件夾嗎? – 2014-11-22 15:20:29

+0

,爲什麼需要你複製php文件?也許一些重定向到第一個會更好? – 2014-11-22 15:27:17

回答

2

可能是這樣的

foreach (new DirectoryIterator('./friends') as $fileInfo) { 
    if($fileInfo->isDir()) { 
     copy('./mypage/homes/index.php', $fileInfo->getPathname() . '/index.php'); 
    } 
} 

您可以添加代碼來測試,如果copy失敗。編號:使用getPathname代替getPath

+0

而且,如果需要遞歸的話:'new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ path),RecursiveIteratorIterator :: CHILD_FIRST);' – 2014-11-22 15:24:47

+0

這似乎在默默地失敗。任何想法可能會出錯? – 2014-11-22 15:41:30

+0

哦,它複製到'朋友'目錄而不是INSIDE'朋友'目錄! – 2014-11-22 15:47:51

相關問題