Recursive Copy of Directory

前端 未结 14 980
花落未央
花落未央 2020-12-08 15:14

On my old VPS I was using the following code to copy the files and directories within a directory to a new directory that was created after the user submitted their form.

14条回答
  •  萌比男神i
    2020-12-08 15:24

    Try something like this:

    $source = "dir/dir/dir";
    $dest= "dest/dir";
    
    mkdir($dest, 0755);
    foreach (
     $iterator = new \RecursiveIteratorIterator(
      new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
      \RecursiveIteratorIterator::SELF_FIRST) as $item
    ) {
      if ($item->isDir()) {
        mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      } else {
        copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      }
    }
    

    Iterator iterate through all folders and subfolders and make copy of files from $source to $dest

提交回复
热议问题