php目录操作实例代码
php  /  管理员 发布于 7年前   362
$dirname = "./final/factapplication"; function listdir($dirname) { 核心:递归的经典应用,以及文件和目录的基本操作。 $srcdir = "../fileupload"; function copydir($srcdir, $dstdir) { while (false !== ($file = readdir($ds))) { } copydir($srcdir, $dstdir); 核心:copy函数。 $dirname = 'a'; function deldir($dirname) { while (false !== ($file = readdir($ds))) { return rmdir($dirname); deldir($dirname); 核心:注意unlink删除的是带path的file。 $dirname = "a"; function dirsize($dirname) { echo dirsize($dirname); 122 在 123 在 原梓番博客 在 博主 在 1111 在
/**
* listdir
*/
header("content-type:text/html;");
$ds = opendir($dirname);
while (false !== ($file = readdir($ds))) {
$path = $dirname.'/'.$file;
if ($file != '.' && $file != '..') {
if (is_dir($path)) {
listdir($path);
} else {
echo $file."
";
}
}
}
closedir($ds);
}
listdir($dirname);
/**
* copydir
*/
$dstdir = "b";
mkdir($dstdir);
$ds = opendir($srcdir);
$path = $srcdir."/".$file;
$dstpath = $dstdir."/".$file;
if ($file != "." && $file != "..") {
if (is_dir($path)) {
copydir($path, $dstpath);
} else {
copy($path, $dstpath);
}
}
}
closedir($ds);
/**
* deldir
*/
$ds = opendir($dirname);
$path = $dirname.'/'.$file;
if($file != '.' && $file != '..') {
if (is_dir($path)) {
deldir($path);
} else {
unlink($path);
}
}
}
closedir($ds);
}
/**
* dirsize
*/
static $tot;
$ds = opendir($dirname);
while (false !== ($file = readdir($ds))) {
$path = $dirname.'/'.$file;
if ($file != '.' && $file != '..') {
if(is_dir($path)) {
dirsize($path);
} else {
$tot = $tot + filesize($path);
}
}
}
return $tot;
closedir($ds);
}
核心:通过判断$tot在哪里返回,理解递归函数。您可能感兴趣的文章:
上一条:
php格式化日期和时间格式化示例分享
下一条:
php无限遍历目录示例
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号