>>  Site Map >>  Forums >>  PHP+MySql

Forum module - topics in forum:



PHP+MySql - Coding mã nguồn mở rât thông dụng để làm web, và hệ thống cơ sỏ dữ liệu rất mạnh duợc ưa chuộng



Tìm file và tính toán thư mục

Find File and Directory Sizes
This php script will search any directory (and all subdirectories under that directory). It will show you the total size of the directory, number of files in the directory, and identify any files > or = to the size specified. When you run this script you will be prompted for the unix directory name and the test file size parameter.

This script can be very helpful if you have multiple ftp users and you want to monitor file or directory sizes.

Put the script below into a file called space.php.

- - Start Script Here - -
Code: :
<?php
if ($_POST['submit'] ) {
$dir = $_POST['dir'];
$test_size = $_POST['size'];
// get info function
function getInfo($dir) {
  global $size;
  global $max_size;
  global $loop_result;
  global $count;
  $dh = opendir($dir);
  while (($file = readdir($dh)) !== false) {
    if ($file != "." and $file != "..") {
      $path = $dir."/".$file;
      if (is_file($path)) {
        $size += filesize($path);
        $count++;
        if (filesize($path) >= $max_size) {
          $print_size = sizeConv(filesize($path));
          $loop_result .="$file $print_size<br>";
        }
      } else {
        if (is_dir($path)) getInfo($path);
      }
    }
  }
  closedir($dh);
  return;
}
// size conversion function
function sizeConv($size) {
  switch ($size) {
    case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
    case ($size>=1024);    $size = round($size/1024) . " KB"; break;
    default:               $size = $size . " bytes"; break;
  }
  return $size;
}
// processing mainline
if (file_exists($dir)) {
  $size = 0; $count = 0; $loop_result = "";
  $max_size = $test_size * 1000;
  getInfo($dir);
  $message .= "<b>Statistics for directory $dir</b><br><br>";
  $display_size = sizeConv($size);
  $message .="Disk space utilization = $display_size<br>";
  $max_size = $test_size * 1000;
  $print_size = sizeconv($max_size);
  $message .= "Total file count = $count<br>";
  if ($loop_result) {
    $message .="<br><b>List of files > or = $print_size:</b><br>";
  } else {
    $loop_result .="No files found > $print_size";
  }
  $message .= $loop_result;
} else {
  $message .= "Directory <b>$dir</b> does not exist, please re-enter a vaild directory path.";
}
// output
echo "$message<br><hr>";
}
$path ="put_your_root_path_here";
// input form
?>
<form action="space.php" method="post" name="inputform">
<b>For a specific directory and all subdirectories, determine:
<br>1) total space used<br>2) number of files
<br>3) all files > or = a certain size</b><br>
<br>Directory <input type=text value="<?php echo $path; ?>" name="dir" size="50">
<br>Size (in KB) <input type=text value="200" name="size" size="6">
<br><input type="submit" name="submit" value="Submit">
</form>

- - End Script Here - -




Search from ALEXA


put your ads here