【问题标题】:Copy one file to all folders in a folder?将一个文件复制到文件夹中的所有文件夹?
【发布时间】:2015-01-20 15:10:09
【问题描述】:

是否可以/容易地将文件从一个地方(比如/mypage/homes/ 中的index.php)复制到另一个文件夹中的文件夹(例如,在friends 中可能有pfoxsam、@ 987654326@) 使用 PHP? 那么文件index.php会被复制到friends/pfoxfriends/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!

【问题讨论】:

  • 如果/friends/one/sub 目录存在怎么办?是否也需要将其放入每个子文件夹中?
  • 为什么需要复制php文件?也许一些重定向到第一个会更好?

标签: php file-io directory copy


【解决方案1】:

可能是这样的

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

您可以添加代码来测试copy 是否失败。

编辑:使用getPathname 而不是getPath

【讨论】:

  • 如果需要递归:new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);
  • 这似乎默默地失败了。有什么想法可能会出错吗?
  • 哦,它复制到“朋友”目录而不是“朋友”内部的目录!
  • @vp_arth 解析错误:语法错误,意外';'在(已编辑)第 2 行
  • 更新后的代码运行良好。接受作为答案,非常感谢!
【解决方案2】:

可能是http://php.net/manual/en/function.copy.php 之类的?

sintax 很简单

copy($file_path, $destination_path);

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2011-02-27
    • 2022-01-13
    相关资源
    最近更新 更多