php基础练习--目录操作

王朝学院·作者佚名  2016-05-25
窄屏简体版  字體: 小  |  中  |  大  |  超大  

php基础练习--目录操作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);

listdir核心:递归的经典应用,以及文件和目录的基本操作。

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

copydir核心: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);

deldir核心:注意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);

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航