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

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

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

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

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

1、复制文件夹

</>code

  1. function copyall($filesrc, $newpath) {
  2.   $dir = opendir($filesrc);
  3.   @mkdir($newpath);
  4.   while (($file = readdir($dir)) !== false) {
  5.     if ($file !== '.' && $file !== '..') {
  6.       if (is_dir($filesrc . '/' . $file)) {
  7.         copyall($filesrc . '/' . $file, $newpath . '/' . $file);
  8.       } else {
  9.         copy($filesrc . '/' . $file, $newpath . '/' . $file);
  10.       }
  11.     }
  12.   }
  13.   closedir($dir);
  14. }

2、移动文件夹

</>code

  1. function moveall($filesrc, $newpath) {
  2.   $dir = opendir($filesrc);
  3.   @mkdir($newpath);
  4.   
  5.   while (($file = readdir($dir)) !== false) {
  6.     if ($file !== '.' && $file !== '..') {
  7.       if (is_dir($filesrc . '/' . $file)) {
  8.         moveall($filesrc . '/' . $file, $newpath . '/' . $file);
  9.       } else {
  10.         rename($filesrc . '/' . $file, $newpath . '/' . $file);
  11.       }
  12.     }
  13.   }
  14.   closedir($dir);
  15. }

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

1、复制文件夹

</>code

  1. function copyall($filesrc, $newpath) {
  2.   $dirStack = array($filesrc);
  3.   $newpathStack = array($newpath);
  4.   while (!empty($dirStack)) {
  5.     $currentSrc = array_pop($dirStack);
  6.     $currentDst = array_pop($newpathStack);
  7.     @mkdir($currentDst);
  8.     $dir = opendir($currentSrc);
  9.     while (($file = readdir($dir)) !== false) {
  10.       if ($file !== '.' && $file !== '..') {
  11.         if (is_dir($currentSrc . '/' . $file)) {
  12.           array_push($dirStack, $currentSrc . '/' . $file);
  13.           array_push($newpathStack, $currentDst . '/' . $file);
  14.         } else {
  15.           copy($currentSrc . '/' . $file, $currentDst . '/' . $file);
  16.         }
  17.       }
  18.     }
  19.     closedir($dir);
  20.   }
  21. }

2、移动文件夹

</>code

  1. function moveall($filesrc, $newpath) {
  2.   $dirStack = array($filesrc);
  3.   $newpathStack = array($newpath);
  4.   while (!empty($dirStack)) {
  5.     $currentSrc = array_pop($dirStack);
  6.     $currentDst = array_pop($newpathStack);
  7.     @mkdir($currentDst);
  8.     $dir = opendir($currentSrc);
  9.     while (($file = readdir($dir)) !== false) {
  10.       if ($file !== '.' && $file !== '..') {
  11.         if (is_dir($currentSrc . '/' . $file)) {
  12.           array_push($dirStack, $currentSrc . '/' . $file);
  13.           array_push($newpathStack, $currentDst . '/' . $file);
  14.         } else {
  15.           rename($currentSrc . '/' . $file, $currentDst . '/' . $file);
  16.         }
  17.       }
  18.     }
  19.     closedir($dir);
  20.   }
  21. }


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

1、复制文件夹

</>code

  1. function copyall($filesrc, $newpath) {
  2.   $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filesrc, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
  3.   
  4.   foreach ($iterator as $file) {
  5.     $target = $newpath . '/' . $iterator->getSubPathName();
  6.     
  7.     if ($file->isDir()) {
  8.       mkdir($target);
  9.     } else {
  10.       copy($file, $target);
  11.     }
  12.   }
  13. }

2、移动文件夹

</>code

  1. function copyall($filesrc, $newpath) {
  2.   $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filesrc, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
  3.   
  4.   foreach ($iterator as $file) {
  5.     $target = $newpath . '/' . $iterator->getSubPathName();
  6.     
  7.     if ($file->isDir()) {
  8.       mkdir($target);
  9.     } else {
  10.       rename($file, $target);
  11.     }
  12.   }
  13. }


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

您可能感兴趣的文章:

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

快速入口

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

其它栏目

· 建站教程
· 365学习

业务咨询

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

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

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