Home > Backend Development > PHP Tutorial > php怎么复制文件夹

php怎么复制文件夹

WBOY
Release: 2016-06-13 12:11:34
Original
1412 people have browsed it

php如何复制文件夹?

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


<?php /*复制当前目录下所有的文件去目标文件夹$cpath 当前目录$dpath 目标目录$type all复制当前所有文件去目标目录,dir复制所有文件至同一目录$i 用来统计数量总*/function copydir_user($cpath,$dpath,&$i,$type=&#39;all&#39;){	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!=&#39;.&#39;&&$file!=&#39;..&#39;){			if(is_dir($cpath.$file)){				$scpath = $cpath.$file.&#39;/&#39;;				$sdpath = $dpath;				if($type == &#39;dir&#39;){					$sdpath = $dpath.$file.&#39;/&#39;;					$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) != &#39;/&#39;){		$dirname.=&#39;/&#39;;			}	if(!file_exists($dirname)){		if($tips){			echo &#39;directory is not exists&#39;;			return false;		}else{			mkdir($dirname);		}	}	return $dirname;}$des_path=&#39;C:/Users/alex/Desktop/test&#39;;//目标目录$cur_path = &#39;E:/xampp/htdocs/bbs/api&#39;; //当前目录$i=0;copydir_user($cur_path,$des_path,$i,&#39;dir&#39;);echo $i;
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template