dongwopu8210 2013-01-08 11:55
浏览 36
已采纳

php复制文件到所有文件夹[关闭]

Hi I want to copy a file to all the folders in a directory

my root folder tree is

i want to copy file in root folder to every target folder

folder structure is

enter image description here

  • 写回答

1条回答 默认 最新

  • doyz51819 2013-01-08 12:19
    关注

    Since you have a static folder structure I'd suggest just storing the different subfolders in an array and loop through that to copy the file

    $rootpath = ".";
    $destpaths = array("/Folder1/Subfolder1/AnotherSubFolder/Target/",
                   "/Folder2/Subfolder1/AnotherSubFolder/Target/",
                   "/Folder3/Subfolder1/AnotherSubFolder/Target/",
                   "/Folder4/Subfolder1/AnotherSubFolder/Target/",
                   "/Folder5/Subfolder1/AnotherSubFolder/Target/",
                   "/Folder6/Subfolder1/AnotherSubFolder/Target/",
                   "/FolderN/Subfolder1/AnotherSubFolder/Target/");
    $file = '/example.txt';
    $copyfrom = $rootpath . $file;
    
    foreach($destpaths as $destpath)
    {
        $copyto = $rootpath . $destpath . $file;
        if (!copy($copyfrom, $copyto)) {
            echo "failed to copy $file";
        }
    }
    

    As Aaron W. suggested and also used glob-php to get recursive paths(folder1,folder2...) i'm placing this code and file same folder and placed that in same root path as folder1,folder2...

    $rootpath = "..";
    $frompath = ".";
    $file = '/example.txt';
    $tosubpath="/Subfolder1/AnotherSubFolder/Target/";
    $copyfrom = $frompath . $file;
    
    listdirs($rootpath,$copyfrom,$tosubpath,$file);
    
    
        function listdirs($dir,$copyfrom,$tosubpath,$file) {
            $dirs = glob($dir . '/*', GLOB_ONLYDIR);
    
        foreach($dirs as $destpath)
        {   
    
            $copyto = $destpath. $tosubpath . $file;
            if (!copy($copyfrom, $copyto)) {
                echo "failed to copy ".$copyfrom." to".$copyto."<br>";
            }else{
                echo $copyfrom." copied to".$copyto."<br>";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么