php删除与复制文件夹及其文件夹下所有文件的实现代码

   2015-11-08 0
核心提示:最近写代码,需要个复制文件夹与及其文件夹下所有文件的功能,有时候需要删除,就可以用这个函数了,通过xcopy与deldir的自定义函数,确实方便多了,特分享下

复制代码 代码如下:

<?php
 /*复制xCopy函数用法:   
  *   xCopy("feiy","feiy2",1):拷贝feiy下的文件到   feiy2,包括子目录   
  *   xCopy("feiy","feiy2",0):拷贝feiy下的文件到   feiy2,不包括子目录   
  *参数说明:   
  *   $source:源目录名   
  *   $destination:目的目录名   
  *   $child:复制时,是不是包含的子目录
  */
function xCopy($source, $destination, $child){
    if (!file_exists($destination))
    {
        if (!mkdir(rtrim($destination, '/'), 0777))
        {
        //$err->add($_LANG['cannt_mk_dir']);
        return false;
        }
        @chmod($destination, 0777);
     }
if(!is_dir($source)){ 
return 0;
}
if(!is_dir($destination)){
mkdir($destination,0777);  
}
$handle=dir($source);
while($entry=$handle->read()){
if(($entry!=".")&&($entry!="..")){
if(is_dir($source."/".$entry)){
if($child)
xCopy($source."/".$entry,$destination."/".$entry,$child);
}
else{
copy($source."/".$entry,$destination."/".$entry);
}
}   
}   
return 1;
}


 /*删除deldir函数用法:   
  *  deldidr("feiy"):删除feiy,包括子目录      
  *参数说明:   
  *   $dir:要删除的目录名   
  */
function deldir($dir) {
if (!file_exists($dir)){return true;
}else{@chmod($dir, 0777);}
  $dh=opendir($dir);
  while ($file=readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(!is_dir($fullpath)) {
          unlink($fullpath);
      } else {
          deldir($fullpath);
      }
    }
  }

  closedir($dh);

  if(rmdir($dir)) {
    return true;
  } else {
    return false;
  }
}
?>

 
标签: 删除 复制 文件夹
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Swift3.0 UICollectionView 删除,拖动
    Swift3.0 UICollectionView 删除,拖动
    UICollectionView实现了一下常见的新闻分类.  附有效果图 近期一直在深入学习swift,实现了CollectionView item的头东与删除,用的都是系统的一些函数方法,看起来比较直观. 第一步:class HotViewController: UIViewController,UICollectionViewDelegate,UICo
    02-09
  • php服务器删除浏览器cookie php清除浏览器缓存
    一、设置cookie的过期时间//将过期时间设为一小时前setcookie("TestCookie", "", time() - 3600);setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);二、设置cookie的值为空setcookie($cookiename, ''
    02-09
  • 小程序 中 如何实现 删除标签
    小程序 中 如何实现 删除标签
     text-decoration: line-through
    02-09
  • 微信小程序:如何删除所有的console.log?
    微信小程序:如何删除所有的console.log?
    使用vscode正则匹配,手动去除1、用vscode打开微信小程序项目2、Edit-----replace in Files1. console.log()加了分号console\.log\(.*?\); 2. console.log()没加分号console\.log\(.*?\) 
    02-09
  • Delphi动态创建、删除按钮
     unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;type  TForm1 = class(TForm)    btnAddButton: TButton;    btnDeleteLast: TButton;    procedure btnAddButtonCli
    02-09
  • 【Perl】Path::File 目录的创建和删除
     1 Path::File中两个函数的基本使用 2 代码举例: 3 more help 1 Path::File中两个函数的基本使用 make_path( $dir1, $dir2, .... )make_path( $dir1, $dir2, ...., \%opts ) #\%opts表示哈希引用mkpath( $dir )mkpath( $dir, $verbose, $mode )mkpath( [$d
    02-09
  • 数据预处理:删除零值数据和缺省数据;众数补全
    %%场景二%%节点数据预处理function [X,Y]=nodedata_pro()%导入班节点数据[num_2ban1,~,raw_2ban1]=xlsread('sen2ban1.xlsx');[num_2ban2,~,raw_2ban2]=xlsread('sen2ban2.xlsx');[num_2ban3,~,raw_2ban3]=xlsread('sen2ban3.xlsx');[num_2ban4,~,raw_2ban4]=x
    02-09
  • matlab 如何把数组A中与数组B中元素相同的元素
    比如:A = [1 3  7  6  4  5  2];B = [3 5 6];如何把数组A中与数组B中的元素相同的元素删除,而剩余的元素还是按照原来的顺序排列?即结果为:[1 7 4 2]。[~, ia] = setdiff(A, B);res = A(sort(ia));欢迎访问我的个人网站:李培冠博客:lpgit.com
    02-09
  • ruby冒泡算法删除店铺下的重复评论
    1 Shop.each do |shop| 2 if !shop.comments.blank? 3 n = shop.comments.length 4 for i in 0..n-1 5 for j in i+1..n-1 6 if shop.comments[i].content == shop.comments[j].content 7 shop.comments[j].destroy 8 end9 end10 end11 end12 end 以上为ruby冒
    02-09
  • Flutter项目删除了相关的dart文件之后运行flutter run或者 F5编译运行时会报这个错误.... were declared as an inputs, but did not exi
    Flutter项目删除了相关的dart文件之后运行flutt
    我所做的项目报的错误是这样的  因为报错内容主要是".... were declared as an inputs, but did not exist. Check the definition of target:kernel_snapshot for errors",应该时删除了文件之后,运行时找不到对应的文件,感觉时缓存问题,没有自动更新到那个
    02-09
点击排行