这个程序为什么不能遍历文件夹上的所有文件和文件夹

WBOY
Release: 2016-06-13 13:12:53
Original
1137 people have browsed it

这个程序为什么不能遍历文件夹下的所有文件和文件夹?
下面是一个计算文件夹下面所有文件及文件夹大小的程序,我让它计算文件夹‘znb1’的大小,此文件夹下面
的结构为
znb1/.buildpath,
znb1/.project,
znb1/223/s.php,
znb1/settings/newfile.php,
znb1/settings/org.eclipse.php.core.prefs
程序运行后只计算出znb1/.buildpath和znb1/.project两个文件的大小
这是为什么?我用的软件为wampserver和editplus程序如下:


function dirsize($dirname) {
$dir = opendir ( $dirname );
while ( ($file = readdir ( $dir )) !== FALSE ) {
$filename=$dirname.'\\'.$file;
if ($file != '.' && $file != '..') {

if (is_dir ( $file )) {

$count += dirsize ( $file );
} else {
$count += filesize($filename);
}
}
}
return $count;
}
echo dirsize ( 'znb1' );

?>

------解决方案--------------------
if (is_dir ( $filename )) {
如果是$file,那么对于这个相对路径,将在当前目录下寻找。未找到自然报false
------解决方案--------------------
set_time_limit(0);
function dirsize($dirname) {
if (is_dir($dirname)) {
$dir = opendir ($dirname);
if ($dir != false) {
while ( ($file = readdir ($dir)) !== FALSE ) {
$filename = $dirname.'\\'.$file;
if (is_file($filename)){
$count[] = filesize($filename)."@".$filename;
} else if (is_dir($filename)){
if ($file != "." && $file != "..") {
dirsize($filename);
}
}
}
}

return $count;
} else {
return false;
}
}
$path = "D:\APMServ5.2.6\www\htdocs\localhost";
$c = dirsize($path);
var_dump($c);
+-----------------------------+
修改了一下,可以计算了,相对路劲没测试过!参考下吧!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!