Home  >  Article  >  Backend Development  >  How to find files of specified size in php in specified directory, _PHP tutorial

How to find files of specified size in php in specified directory, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:47853browse

php method to find files of specified size in specified directory,

The example in this article describes how PHP searches for files of a specified size in a specified directory. Share it with everyone for your reference. The specific implementation method is as follows:

The principle of PHP to find the file size is to traverse the directory and then use filesize to calculate the file size, and then we just add one more judgment. Here are some examples.

Let’s first look at traversing the directory, the code is as follows:

Copy code The code is as follows:
function tree($directory)
{
$mydir = dir($directory);
echo "
    n";
    while($file = $mydir->read())
    {
    if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
    {
    echo "
  • $file
  • n";
    tree("$directory/$file");
    }
    else
    echo "
  • $file
  • n";
    }
    echo "
n";
$mydir->close();
}
//Start running

echo "

The directory is pink


n";
tree("./nowamagic");

This only displays the files in all directories, but if we want to determine the size, we need to add the round(filesize($cpath)/1024,1) function, so that we can display the file size after getting the size. The code is as follows:
Copy code The code is as follows:
header("Content-Type:text/html;charset=gbk");
set_time_limit(0);
$dirpath=dirname(__FILE__);
//bytes
$limitByte=1024*110;
//Change here to the minimum size of your appropriate search file, in bytes. 1024*100 means 1024*100 bytes, which is 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]."";
}
?>


Finally, I attach a byte calculation function for you. This can be converted. The code is as follows:
Copy the code The code is as follows:
//Convert the number of bytes into units
/* The principle is to use logarithms to find out how many powers of 1024 the number of bytes to be converted is.
*In fact, it is to use the characteristics of logarithm to determine the unit.
*/
function size2mb($size,$digits=2){ //digits, how many decimal places to keep
$unit= array('','K','M','G','T','P');//The unit array must be in 1024-digit order.
$base= 1024;//The base of the logarithm
$i = floor(log($size,$base));//The number of bytes is logarithmic to 1024, and the value is rounded down.
Return round($size/pow($base,$i),$digits).' '.$unit[$i] . 'B';
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/919264.htmlTechArticleHow to find a file of a specified size in a specified directory using php. This example describes how to find a file of a specified size in a specified directory using php. method. Share it with everyone for your reference. The specific implementation methods are as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn