php查找文件大小的原理是遍历目录然后再利用filesize来计算文件大小,然后我们再加一判断就可以了,下面整理了一些例子。
我们先来看遍历目录
<?php 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 id="目录为粉红色">目录为粉红色</h2><br>n"; tree("./phprm"); ?>
这样只是把所有目录下的文件显示了,但我们要判断大小需加上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>"; } } } } } closedir($h); } foreach ($arrRes as $k => $v) { $arrTmp[$k] = $v[1]; } arsort($arrTmp); foreach ($arrTmp as $k => $v) { echo "<p>" . str_replace($dirpath, '', $arrRes[$k][0]) . "<br />" . $arrRes[$k][1] . "</p>"; } ?>
最后给大家附一个字节计算函数,这个可以转换
<?php //字节数转换成带单位的 /* 原理是利用对数求出欲转换的字节数是1024的几次方。 * 其实就是利用对数的特性确定单位。 */ function size2mb($size, $digits = 2) { //digits,要保留几位小数 $unit = array( '', 'K', 'M', 'G', 'T', 'P' ); //单位数组,是必须1024进制依次的哦。 $base = 1024; //对数的基数 $i = floor(log($size, $base)); //字节数对1024取对数,值向下取整。 return round($size / pow($base, $i) , $digits) . ' ' . $unit[$i] . 'B'; } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.