php如何复制文件夹?

php只有复制文件函数copy()。闲来无事用递归写了一个复制目录的递归函数来练练手,还花了我不少的时间。看来还是得勤练习多思考。


<?php
/*
复制当前目录下所有的文件去目标文件夹
$cpath 当前目录
$dpath 目标目录
$type all复制当前所有文件去目标目录,dir复制所有文件至同一目录
$i 用来统计数量总
*/
function copydir_user($cpath,$dpath,&$i,$type='all'){
	if($i == 0){
		if(!($cpath = judge_dir($cpath,true))){
			return false;
		}
		$dpath = judge_dir($dpath,false);
	}

	$handle = opendir($cpath);
	while (false !== ($file = readdir($handle))) {
		if($file!='.'&&$file!='..'){
			if(is_dir($cpath.$file)){
				$scpath = $cpath.$file.'/';
				$sdpath = $dpath;
				if($type == 'dir'){
					$sdpath = $dpath.$file.'/';
					$sdpath = judge_dir($sdpath,false);
				}
				copydir_user($scpath,$sdpath,$i,$type);
			}else{
				$current = $cpath.$file;
				$source = $dpath.$file;
 				copy($current,$source);
				$i++;
			}
	   }
   
	}
}

function judge_dir($dirname,$tips=true){
	if(substr($dirname,strlen($dirname)-1) != '/'){
		$dirname.='/';		
	}
	if(!file_exists($dirname)){
		if($tips){
			echo 'directory is not exists';
			return false;
		}else{
			mkdir($dirname);
		}
	}
	return $dirname;

}

$des_path='C:/Users/alex/Desktop/test';//目标目录
$cur_path = 'E:/xampp/htdocs/bbs/api'; //当前目录
$i=0;
copydir_user($cur_path,$des_path,$i,'dir');
echo $i;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值