2014-11-22 172 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

相关问题