猿问

如何在PHP中获取目录大小

function foldersize($path) {

  $total_size = 0;

  $files = scandir($path);


  foreach($files as $t) {

    if (is_dir(rtrim($path, '/') . '/' . $t)) {

      if ($t<>"." && $t<>"..") {

          $size = foldersize(rtrim($path, '/') . '/' . $t);


          $total_size += $size;

      }

    } else {

      $size = filesize(rtrim($path, '/') . '/' . $t);

      $total_size += $size;

    }

  }

  return $total_size;

}


function format_size($size) {

  $mod = 1024;

  $units = explode(' ','B KB MB GB TB PB');

  for ($i = 0; $size > $mod; $i++) {

    $size /= $mod;

  }


  return round($size, 2) . ' ' . $units[$i];

}


$SIZE_LIMIT = 5368709120; // 5 GB


$sql="select * from users order by id";

$result=mysql_query($sql);


while($row=mysql_fetch_array($result)) {

  $disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);


  $disk_remaining = $SIZE_LIMIT - $disk_used;

  print 'Name: ' . $row['name'] . '<br>';


  print 'diskspace used: ' . format_size($disk_used) . '<br>';

  print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';

}

php disk_total_space


在脚本执行完成之前,为什么处理器使用率会过高或100%升高?可以做些什么来优化它吗?还是有其他方法可以检查文件夹和其中的文件夹大小?


泛舟湖上清波郎朗
浏览 686回答 3
3回答

杨__羊羊

以下是其他地方提供的其他解决方案:如果在Windows主机上:<?&nbsp; &nbsp; $f = 'f:/www/docs';&nbsp; &nbsp; $obj = new COM ( 'scripting.filesystemobject' );&nbsp; &nbsp; if ( is_object ( $obj ) )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $ref = $obj->getfolder ( $f );&nbsp; &nbsp; &nbsp; &nbsp; echo 'Directory: ' . $f . ' => Size: ' . $ref->size;&nbsp; &nbsp; &nbsp; &nbsp; $obj = null;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo 'can not create object';&nbsp; &nbsp; }?>否则,如果在Linux主机上:<?&nbsp; &nbsp; $f = './path/directory';&nbsp; &nbsp; $io = popen ( '/usr/bin/du -sk ' . $f, 'r' );&nbsp; &nbsp; $size = fgets ( $io, 4096);&nbsp; &nbsp; $size = substr ( $size, 0, strpos ( $size, "\t" ) );&nbsp; &nbsp; pclose ( $io );&nbsp; &nbsp; echo 'Directory: ' . $f . ' => Size: ' . $size;?>

森栏

function GetDirectorySize($path){&nbsp; &nbsp; $bytestotal = 0;&nbsp; &nbsp; $path = realpath($path);&nbsp; &nbsp; if($path!==false && $path!='' && file_exists($path)){&nbsp; &nbsp; &nbsp; &nbsp; foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bytestotal += $object->getSize();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $bytestotal;}与詹妮丝·金塔纳(Janith Chinthana)提出的想法相同。有一些修复:转换$path为realpath仅当路径有效且文件夹存在时才执行迭代跳过.和..文件性能优化

红糖糍粑

一个纯PHP的例子。<?php&nbsp; &nbsp; $units = explode(' ', 'B KB MB GB TB PB');&nbsp; &nbsp; $SIZE_LIMIT = 5368709120; // 5 GB&nbsp; &nbsp; $disk_used = foldersize("/webData/users/vdbuilder@yahoo.com");&nbsp; &nbsp; $disk_remaining = $SIZE_LIMIT - $disk_used;&nbsp; &nbsp; echo("<html><body>");&nbsp; &nbsp; echo('diskspace used: ' . format_size($disk_used) . '<br>');&nbsp; &nbsp; echo( 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>');&nbsp; &nbsp; echo("</body></html>");function foldersize($path) {&nbsp; &nbsp; $total_size = 0;&nbsp; &nbsp; $files = scandir($path);&nbsp; &nbsp; $cleanPath = rtrim($path, '/'). '/';&nbsp; &nbsp; foreach($files as $t) {&nbsp; &nbsp; &nbsp; &nbsp; if ($t<>"." && $t<>"..") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentFile = $cleanPath . $t;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (is_dir($currentFile)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $size = foldersize($currentFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total_size += $size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $size = filesize($currentFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total_size += $size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; return $total_size;}function format_size($size) {&nbsp; &nbsp; global $units;&nbsp; &nbsp; $mod = 1024;&nbsp; &nbsp; for ($i = 0; $size > $mod; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $size /= $mod;&nbsp; &nbsp; }&nbsp; &nbsp; $endIndex = strpos($size, ".")+3;&nbsp; &nbsp; return substr( $size, 0, $endIndex).' '.$units[$i];}?>
随时随地看视频慕课网APP
我要回答