Home > Backend Development > PHP Tutorial > php递归复制文件和目录,该如何解决

php递归复制文件和目录,该如何解决

WBOY
Release: 2016-06-13 10:10:13
Original
769 people have browsed it

php递归复制文件和目录
 function del($path){
$handle = opendir($path);
readdir($handle);
readdir($handle); while(false !== ($file = readdir($handle))){
$file = $path."/".$file;

if(is_dir($file)){
del($file);
}else{
if(unlink($file)){
echo "文件".$file."成功"."\r\n";
}else{
echo "文件".$file."失败"."\r\n";
}
}
}
closedir($handle);
if(rmdir($path)){
echo "目录".$path."删除成功"."\r\n";
}else{
echo "目录".$path."删除失败"."\r\n";
}

}

del('d:/123');
 
?>

为什么用两次readdir($handle);?求解,详细点,用一次为什么出错?



------解决方案--------------------
一个目录下有两个特殊目录 . 和 .. ,代表本目录和上级目录,这两个readdir($handle)就是把这个两特殊目录读取后不用来跳过,这样才能正确操作指定目录下的文件和子目录的。

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