PHP遍历目录返回统计目录大小实例_PHP教程

WBOY
Lepaskan: 2016-07-13 10:28:45
asal
824 orang telah melayarinya

分享一个 PHP遍历目录并返回统计目录大小的方法。
代码:

<?<span>php 
</span><span>$dirname</span> = "test1"<span>; 
</span><span>//</span><span>mkdir($dirname);

//遍历一层目录 </span>
<span>function</span> listdir(<span>$dirname</span><span>) { 
</span><span>$ds</span> = <span>opendir</span>(<span>$dirname</span><span>); 
</span><span>while</span>(<span>$file</span> = <span>readdir</span>(<span>$ds</span><span>)) { 
</span><span>$path</span> = <span>$dirname</span>.'/'.<span>$file</span><span>; 
</span><span>if</span>(<span>is_dir</span>(<span>$file</span><span>)) { 
</span><span>echo</span> "DIR:".<span>$file</span>."<br>"<span>; 
</span><span>if</span>(<span>$file</span> != "." && <span>$file</span> != ".."<span>) { 
listdir(</span><span>$file</span><span>); 
} 
} 
</span><span>else</span><span> { 
</span><span>echo</span> "FILE:".<span>$file</span> . "<br>"<span>; 
} 
} 
}

</span><span>function</span> totdir(<span>$dirname</span>) { <span>//</span><span>对listdir稍加修改 </span>
<span>static</span> <span>$tot</span> = 0<span>; 
</span><span>$ds</span> = <span>opendir</span>(<span>$dirname</span><span>); 
</span><span>while</span>(<span>$file</span> = <span>readdir</span>(<span>$ds</span><span>)) { 
</span><span>$path</span> = <span>$dirname</span>.'/'.<span>$file</span><span>; 
</span><span>if</span>(<span>is_dir</span>(<span>$file</span><span>)) { 
</span><span>//</span><span>echo "DIR:".$file."<br>"; </span>
<span>if</span>(<span>$file</span> != "." && <span>$file</span> != ".."<span>) { 
</span><span>$tot</span> += totdir(<span>$file</span><span>); 
} 
} www</span>.jbxue.<span>com
</span><span>else</span><span> { 
</span><span>//</span><span>echo "FILE:".$file . "<br>"; </span>
<span>$tot</span> += <span>filesize</span>(<span>$path</span><span>); 
} 
}

</span><span>//</span><span>返回总计 </span>
<span>return</span> <span>$tot</span><span>; 
}

listdir(</span><span>$dirname</span><span>);

</span><span>echo</span> totdir(<span>$dirname</span>)." bytes"<span>;

</span>?>
Salin selepas log masuk

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/781159.htmlTechArticle分享一个 PHP遍历目录并返回统计目录大小的方法。 代码: ? php $dirname = "test1" ; // mkdir($dirname);//遍历一层目录 function listdir( $dirname ) { $ds...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan