您现在的位置: 365建站网 > 365文章 > 复制移动整个文件夹php代码函数三种写法

复制移动整个文件夹php代码函数三种写法

文章来源:365jz.com     点击数:212    更新时间:2024-07-27 09:24   参与评论

复制移动整个文件夹php代码三种写法:

一、使用递归的方式复制和移动文件夹

1、复制文件夹

function copyall($filesrc, $newpath) {
  $dir = opendir($filesrc);
  @mkdir($newpath);
  while (($file = readdir($dir)) !== false) {
    if ($file !== '.' && $file !== '..') {
      if (is_dir($filesrc . '/' . $file)) {
        copyall($filesrc . '/' . $file, $newpath . '/' . $file);
      } else {
        copy($filesrc . '/' . $file, $newpath . '/' . $file);
      }
    }
  }
  closedir($dir);
}

2、移动文件夹

function moveall($filesrc, $newpath) {
  $dir = opendir($filesrc);
  @mkdir($newpath);
  
  while (($file = readdir($dir)) !== false) {
    if ($file !== '.' && $file !== '..') {
      if (is_dir($filesrc . '/' . $file)) {
        moveall($filesrc . '/' . $file, $newpath . '/' . $file);
      } else {
        rename($filesrc . '/' . $file, $newpath . '/' . $file);
      }
    }
  }
  closedir($dir);
}

二、使用迭代的方式复制和移动文件夹

1、复制文件夹

function copyall($filesrc, $newpath) {
  $dirStack = array($filesrc);
  $newpathStack = array($newpath);
  while (!empty($dirStack)) {
    $currentSrc = array_pop($dirStack);
    $currentDst = array_pop($newpathStack);
    @mkdir($currentDst);
    $dir = opendir($currentSrc);
    while (($file = readdir($dir)) !== false) {
      if ($file !== '.' && $file !== '..') {
        if (is_dir($currentSrc . '/' . $file)) {
          array_push($dirStack, $currentSrc . '/' . $file);
          array_push($newpathStack, $currentDst . '/' . $file);
        } else {
          copy($currentSrc . '/' . $file, $currentDst . '/' . $file);
        }
      }
    }
    closedir($dir);
  }
}

2、移动文件夹

function moveall($filesrc, $newpath) {
  $dirStack = array($filesrc);
  $newpathStack = array($newpath);
  while (!empty($dirStack)) {
    $currentSrc = array_pop($dirStack);
    $currentDst = array_pop($newpathStack);
    @mkdir($currentDst);
    $dir = opendir($currentSrc);
    while (($file = readdir($dir)) !== false) {
      if ($file !== '.' && $file !== '..') {
        if (is_dir($currentSrc . '/' . $file)) {
          array_push($dirStack, $currentSrc . '/' . $file);
          array_push($newpathStack, $currentDst . '/' . $file);
        } else {
          rename($currentSrc . '/' . $file, $currentDst . '/' . $file);
        }
      }
    }
    closedir($dir);
  }
}


三、使用 PHP 的函数库(FilesystemIterator 和 RecursiveDirectoryIterator)复制和移动文件夹

1、复制文件夹

function copyall($filesrc, $newpath) {
  $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filesrc, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
  
  foreach ($iterator as $file) {
    $target = $newpath . '/' . $iterator->getSubPathName();
    
    if ($file->isDir()) {
      mkdir($target);
    } else {
      copy($file, $target);
    }
  }
}

2、移动文件夹

function copyall($filesrc, $newpath) {
  $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filesrc, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
  
  foreach ($iterator as $file) {
    $target = $newpath . '/' . $iterator->getSubPathName();
    
    if ($file->isDir()) {
      mkdir($target);
    } else {
      rename($file, $target);
    }
  }
}


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

您可能感兴趣的文章:

发表评论 (212人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号