Jump to content

Scanning the public_html folder for total used size of an extenssion


Recommended Posts

Hey guys i need to create a function that scans the whole website and gets the total used size for each extenssion inside an array to then be able to use it this way

 

<?php
echo 'Total space used for MP3 '.$scan[mp3][size].' bytes';
echo 'Total space used for JPG '.$scan[jpg][size].' bytes';
echo 'Total space used for PHP '.$scan[code=php:0][size].' bytes';
/// [...]
?>

I modified an existing directory size script and came up with this

<?php

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

}

function getFileSizeByExtension($path, $extensions=array())
{
    $ignore = array('.', '..', '.htaccess');

    foreach($extensions as $ext)
    {
        $totalsize[$ext]['size']  = 0; // file size in bytes
        $totalsize[$ext]['count'] = 0; // file counter
    }

    if ($handle = opendir ($path))
    {
        while (false !== ($file = readdir($handle)))
        {
            $nextpath = $path . '/' . $file;

            if (!in_array($file, $ignore))
            {
                $nextpath_ext = pathinfo($nextpath, PATHINFO_EXTENSION);

                if (is_dir ($nextpath))
                {
                    $result = getFileSizeByExtension($nextpath, $extensions);

                    foreach($result as $ext => $data)
                    {
                        $totalsize[$ext]['size']  += $data['size'];
                        $totalsize[$ext]['count'] += $data['count'];
                    }

                }
                elseif (is_file ($nextpath) && in_array($nextpath_ext, $extensions))
                {
                    $totalsize[$nextpath_ext]['size'] += filesize ($nextpath);
                    $totalsize[$nextpath_ext]['count']++;
                }
            }
        }
    }

    closedir ($handle);

    return $totalsize;
}

$search_path = $_SERVER['DOCUMENT_ROOT'];
$search_extensions = array('php', 'jpg', 'mp3');

$filesizes = getFileSizeByExtension($search_path, $search_extensions);

foreach($filesizes as $ext => $data)
    echo $data['count'] . ' ' . strtoupper($ext) . ' files used ' . sizeFormat($data['size']) .' of disk space<br />';
?>

Thanks wildteen88 :-)

It's working great for jpg, png and gif but for some reasons when it gets to mp3 it goes off the chart ... it outputs 61 GB... Cpanel is repporting that the hole domain (MySQL included) uses only 34 GB

 

Array
(
    [jpg] => Array
        (
            [size] => 98127430
            [count] => 5140
        )

    [png] => Array
        (
            [size] => 1450028
            [count] => 326
        )

    [gif] => Array
        (
            [size] => 1026727
            [count] => 137
        )

    [mp3] => Array
        (
            [size] => 64418466742
            [count] => 9831
        )

)

I tryed changing the filesize function to this one

 

function fsize($file) {
  // filesize will only return the lower 32 bits of
  // the file's size! Make it unsigned.
  $fmod = filesize($file);
  if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);

  // find the upper 32 bits
  $i = 0;

  $myfile = fopen($file, "r");

  // feof has undefined behaviour for big files.
  // after we hit the eof with fseek,
  // fread may not be able to detect the eof,
  // but it also can't read bytes, so use it as an
  // indicator.
  while (strlen(fread($myfile, 1)) === 1) {
    fseek($myfile, PHP_INT_MAX, SEEK_CUR);
    $i++;
  }

  fclose($myfile);

  // $i is a multiplier for PHP_INT_MAX byte blocks.
  // return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
  if ($i % 2 == 1) $i--;

  // add the lower 32 bit to our PHP_INT_MAX multiplier
  return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}

 

But now the page takes for ever to load ... It's been loading for 13 minutes now ... and it keeps loading lol

i tryed a new file size function and now i am back to the same result i had by using the php filesize function ... i don't get it thought ... theres no way there is 60 GB of MP3 when the total used space on the server is 34 GB ...

 

function sizeFormat($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}

function getSize($file) {
        $size = filesize($file);
        if ($size < 0)
            if (!(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'))
                $size = trim(`stat -c%s $file`);
            else{
                $fsobj = new COM("Scripting.FileSystemObject");
                $f = $fsobj->GetFile($file);
                $size = $file->Size;
            }
        return $size;
} 

function getFileSizeByExtension($path, $extensions=array()){
    $ignore = array('.', '..', '.htaccess');

    foreach($extensions as $ext){
        $totalsize[$ext]['size']  = 0; // file size in bytes
        $totalsize[$ext]['count'] = 0; // file counter
    }

    if ($handle = opendir ($path)){
        while (false !== ($file = readdir($handle))){
            $nextpath = $path . '/' . $file;

            if (!in_array($file, $ignore)){
                $nextpath_ext = pathinfo($nextpath, PATHINFO_EXTENSION);

                if (is_dir ($nextpath)){
                    $result = getFileSizeByExtension($nextpath, $extensions);

                    foreach($result as $ext => $data){
                        $totalsize[$ext]['size']  += $data['size'];
                        $totalsize[$ext]['count'] += $data['count'];
                    }

                }
                elseif (is_file ($nextpath) && in_array($nextpath_ext, $extensions)){
                    $totalsize[$nextpath_ext]['size'] += getSize ($nextpath);
                    $totalsize[$nextpath_ext]['count']++;
                }
            }
        }
    }

    closedir ($handle);

    return $totalsize;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.