php查找指定目录指定大小的文件程序
php查找文件大小的原理是遍历目录然后再利用filesize来计算文件大小,然后我们再加一判断就可以了,下面整理了一些例子.
我们先来看遍历目录,代码如下:
function tree( $directory ) { $mydir = dir( $directory ); echo "<ul>n" ; while ( $file = $mydir ->read()) { if (( is_dir ( "$directory/$file" )) AND ( $file != "." ) AND ( $file != ".." )) { echo "<li><font color=" #ff00cc "><b>$file</b></font></li>n" ; tree("$directory/$file" ); } else echo "<li>$file</li>n" ; } echo "</ul>n" ; $mydir ->close(); } //开始运行 echo "<h2>目录为粉红色</h2><br>n" ; tree("./nowamagic" ); 这样只是把所有目录下的文件显示了,但我们要判断大小需加上round(filesize($cpath)/1024,1)函数了,这样我们获取大小之后就可以显示文件大小了,代码如下:
<?php header("Content-Type:text/html;charset=gbk" ); set_time_limit(0); $dirpath =dirname( __FILE__ ); //bytes $limitByte =1024*110; //这里改成你合适的查找文件最低大小,单位为字节。1024*100表示 1024*100字节,即100KB $arrRes = $arrTmp = array (); showMaxFile($dirpath , $limitByte ); function showMaxFile( $path , $limitByte ){ global $arrRes ; $h =opendir( $path ); if ( $h ){ while (false !== ( $file = readdir( $h ))) { if ( $file != '.' && $file != '..' ){ $cpath = $path . '/' . $file ; if ( is_dir ( $cpath )){ showMaxFile($cpath , $limitByte ); }else { if ( filesize ( $cpath ) > $limitByte ){ $arrRes []= array ( $cpath , round ( filesize ( $cpath )/1024,1)); //echo "<p>{$cpath}<br />".(filesize($cpath) / 1024)."KB</p>"; } }