当前位置: 代码迷 >> PHP >> php怎么复制文件夹
  详细解决方案

php怎么复制文件夹

热度:49   发布时间:2016-04-28 18:08:25.0
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;

  相关解决方案