How to show the size of folder using PHP with progressbar

To show the details of folder, first of all we check that, it is folder of file. If it is a folder then we count the number of files present in that folder and add the size of these files.

In which we make two functions first is find the details of the folder and second is show the file size in bytes, Mega bytes or in Gigabyte.

To get the details of folder:

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'];
        }
        elseif (is_file ($nextpath))
        {
          $totalsize += filesize ($nextpath);
          $totalcount++;
        }
      }
    }
  }
  closedir ($handle);
  $total['size'] = $totalsize;
  $total['count'] = $totalcount;
  $total['dircount'] = $dircount;
  return $total;
}

To get the actual size of the folder:

function sizeFormat($size)
{
    if($size<1024)
    {
        return $size." bytes";
    }
    else if($size<(1024*1024))
    {
        $size1=round($size/1024,1);
        return $size1." KB";
    }
    else if($size<(1024*1024*1024))
    {
        $size1=round($size/(1024*1024),1);
        return $size1." MB";
    }
    else
    {
        $size1=round($size/(1024*1024*1024),1);
        return $size1." GB";
    }

}

Calculate the percentage of the Folder:

$percent = (sizeFormat1($ar['size']) * 100 ) / 681392000 ;

 

To download complete script with progress bar click here

Share This Post

Post Comment