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."<br>";
}
}
}
closedir($ds);
}
listdir($dirname);
核心:递归的经典应用,以及文件和目录的基本操作。
<?php
/**
* 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函数。
<?php
/**
* 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。
<?php
/**
* 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在哪里返回,理解递归函数。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程