Heim  >  Artikel  >  Backend-Entwicklung  >  这个程序为什么不能遍历文件夹上的所有文件和文件夹

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

WBOY
WBOYOriginal
2016-06-13 10:46:26763Durchsuche

这个程序为什么不能遍历文件夹下的所有文件和文件夹?
下面是一个计算文件夹下面所有文件及文件夹大小的程序,我让它计算文件夹‘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);
+-----------------------------+
修改了一下,可以计算了,相对路劲没测试过!参考下吧!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn