The example in this article tells you how PHP looks for a specified size file under a specified directory. Share to everyone for your reference. The implementation methods are as follows:
PHP to find the size of the file is to traverse the directory and then use FileSize to calculate the size of the file, and then we add a judgment on it, the following sorted out some examples.
Let's take a look at the directory, the code is as follows:
Copy Code code as follows:
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 ();
}
Start running
echo "Tree ("./nowamagic");
This only shows the files in all directories, but we have to determine the size of the round (FileSize ($cpath)/1024,1) function, so that we can get the size of the file size, the code is as follows:
Copy Code code as follows:
<?php
Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK");
Set_time_limit (0);
$dirpath =dirname (__file__);
bytes
$limitByte =1024*110;
Here you are. The minimum size of the lookup file, in bytes. 1024*100 represents 1024*100 bytes, that 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 "<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> ";
}
?>
Finally, we enclose a byte-counting function, which can be converted with the following code:
Copy Code code as follows:
Converts the number of bytes into a unit
/* principle is to use the logarithm to find the number of bytes to convert is 1024 of the square.
* In fact, the use of logarithmic characteristics to determine units.
*/
function size2mb ($size, $digits =2) {//digits, to keep a few decimal places
$unit = Array (', ' K ', ' M ', ' G ', ' T ', ' P ');/unit array, it must be 1024 in sequence.
$base = cardinality of the 1024;//logarithm
$i = Floor ($size, $base);//number of bytes to 1024 logarithm, the value is rounded down.
Return round ($size/pow ($base, $i), $digits). ' '. $unit [$i]. ' B ';
}
I hope this article will help you with your PHP program design.