PHP –获取目录的大小

发布于 2025-01-02 13:09:06 字数 227 浏览 1 评论 0原文

在 PHP 中获取目录大小的最佳方法是什么?我正在寻找一种轻量级的方法来执行此操作,因为我将使用它的目录非常巨大。

SO 上已经有一个关于此的问题,但它已经存在三年了,而且解决方案已经过时了(现在,出于安全原因,fopen 已被禁用。)

What is the best way to get the size of a directory in PHP? I'm looking for a lightweight way to do this since the directories I'll use this for are pretty huge.

There already was a question about this on SO, but it's three years old and the solutions are outdated.(Nowadays fopen is disabled for security reasons.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2025-01-09 13:09:06

您可以使用 RecursiveDirectoryIterator 吗?

$bytes = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $i) 
{
  $bytes += $i->getSize();
}

Is the RecursiveDirectoryIterator available to you?

$bytes = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $i) 
{
  $bytes += $i->getSize();
}
成熟稳重的好男人 2025-01-09 13:09:06

您可以尝试使用 unix 命令 du:

$output = du -s $folder;

执行运算符。来自: http://www.darian-brown.com/get-php- directory-size/

或者编写一个自定义函数来合计目录中所有文件的文件大小:

function getDirectorySize($path)
{
  $totalsize = 0;
  $totalcount = 0;
  $dircount = 0;
 if($handle = opendir($path))
 {
    while (false !== ($file = readdir($handle)))
    {
      $nextpath = $path . '/' . $file;
      if($file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if(is_dir($nextpath))
       {
         $dircount++;
         $result = getDirectorySize($nextpath);
         $totalsize += $result['size'];
          $totalcount += $result['count'];
         $dircount += $result['dircount'];
       }
       else if(is_file ($nextpath))
       {
          $totalsize += filesize ($nextpath);
          $totalcount++;
       }
     }
   }
 }
 closedir($handle);
 $total['size'] = $totalsize;
 $total['count'] = $totalcount;
 $total['dircount'] = $dircount;
 return $total;
}

You could try the execution operator with the unix command du:

$output = du -s $folder;

FROM: http://www.darian-brown.com/get-php-directory-size/

Or write a custom function to total the filesize of all the files in the directory:

function getDirectorySize($path)
{
  $totalsize = 0;
  $totalcount = 0;
  $dircount = 0;
 if($handle = opendir($path))
 {
    while (false !== ($file = readdir($handle)))
    {
      $nextpath = $path . '/' . $file;
      if($file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if(is_dir($nextpath))
       {
         $dircount++;
         $result = getDirectorySize($nextpath);
         $totalsize += $result['size'];
          $totalcount += $result['count'];
         $dircount += $result['dircount'];
       }
       else if(is_file ($nextpath))
       {
          $totalsize += filesize ($nextpath);
          $totalcount++;
       }
     }
   }
 }
 closedir($handle);
 $total['size'] = $totalsize;
 $total['count'] = $totalcount;
 $total['dircount'] = $dircount;
 return $total;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文