有关目录的php代码,php目录操作实例代码

/**

* listdir

*/

header("content-type:text/html;charset=utf-8");

$dirname = "./final/factapplication";

function listdir($dirname) {

$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

*/

$srcdir = "../fileupload";

$dstdir = "b";

function copydir($srcdir, $dstdir) {

mkdir($dstdir);

$ds = opendir($srcdir);

while (false !== ($file = readdir($ds))) {

$path = $srcdir."/".$file;

$dstpath = $dstdir."/".$file;

if ($file != "." && $file != "..") {

if (is_dir($path)) {

copydir($path, $dstpath);

} else {

copy($path, $dstpath);

}

}

}

closedir($ds);

}

copydir($srcdir, $dstdir);

核心:copy函数。

/**

* deldir

*/

$dirname = 'a';

function deldir($dirname) {

$ds = opendir($dirname);

while (false !== ($file = readdir($ds))) {

$path = $dirname.'/'.$file;

if($file != '.' && $file != '..') {

if (is_dir($path)) {

deldir($path);

} else {

unlink($path);

}

}

}

closedir($ds);

return rmdir($dirname);

}

deldir($dirname);

核心:注意unlink删除的是带path的file。

/**

* dirsize

*/

$dirname = "a";

function dirsize($dirname) {

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);

}

echo dirsize($dirname);

核心:通过判断$tot在哪里返回,理解递归函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值