php查找指定目录下指定大小文件的方法,
本文实例讲述了php查找指定目录下指定大小文件的方法。分享给大家供大家参考。具体实现方法如下:
php查找文件大小的原理是遍历目录然后再利用filesize来计算文件大小,然后我们再加一判断就可以了,下面整理了一些例子.
我们先来看遍历目录,代码如下:
复制代码 代码如下:
function tree($directory)
{
$mydir = dir($directory);
echo "
- n";
- $file n";
- $file n";
while($file = $mydir->read())
{
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
{
echo "
tree("$directory/$file");
}
else
echo "
}
echo "
$mydir->close();
}
//开始运行
echo "
目录为粉红色
n";
tree("./nowamagic");
这样只是把所有目录下的文件显示了,但我们要判断大小需加上round(filesize($cpath)/1024,1)函数了,这样我们获取大小之后就可以显示文件大小了,代码如下:
复制代码 代码如下:
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 "
{$cpath}
".(filesize($cpath) / 1024)."KB
}
}
}
}
}
closedir($h);
}
foreach($arrRes as $k=>$v){
$arrTmp[$k]=$v[1];
}
arsort($arrTmp);
foreach($arrTmp as $k=>$v){
echo "
".str_replace($dirpath,'',$arrRes[$k][0])."
".$arrRes[$k][1]."
}
?>
最后给大家附一个字节计算函数,这个可以转换,代码如下:
复制代码 代码如下:
//字节数转换成带单位的
/* 原理是利用对数求出欲转换的字节数是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';
}
希望本文所述对大家的PHP程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor