1

okay so I have a php file sitting on main root and I am trying to get the filesize of the files in a lower folder on my server.

Example: http://website.com/myphpfile.php trying to access: http://website.com/movies/[with everything in in]

I keep getting 0 or some random number but I can't get it to produce the size for each file.

 // Opens directory
$myDirectory = opendir("./movies"); 

// Gets each entry
while($entryName = readdir($myDirectory)) {
   $dirArray[] = $entryName;
}

I am using this snippet:

function size_readable($size, $max = null, $system = 'si', $retstring = '%01.2f %s')
{
    // Pick units
    $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB');
    $systems['si']['size']   = 1000;
    $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
    $systems['bi']['size']   = 1024;
    $sys = isset($systems[$system]) ? $systems[$system] : $systems['si'];

    // Max unit to display
    $depth = count($sys['prefix']) - 1;
    if ($max && false !== $d = array_search($max, $sys['prefix'])) {
        $depth = $d;
    }

    // Loop
    $i = 0;
    while ($size >= $sys['size'] && $i < $depth) {
        $size /= $sys['size'];
        $i++;
    }

    return sprintf($retstring, $size, $sys['prefix'][$i]);
}

My question is how do I call those files?

I know this is wrong here but what should it be?

$thefilesize = size_readable(filesize($entryName));

OR

Can someone write/edit this code to help me understand what is going on and where/what it's grabbing?

Joe Salmi
  • 79
  • 10
  • Where are you calling size_readable? What is the value of $file? – Robbert Jan 29 '14 at 03:10
  • I'm calling size_readable inside the loop and $file is null. I didn't mean to put that in there. Let me edit that section. It should read: $thefilesize = size_readable(filesize($entryName)); – Joe Salmi Jan 29 '14 at 03:15

1 Answers1

3

$entryName will only refer to the file name. If you want to refer to the file, you need to include the directory as well.

$thefilesize = size_readable(filesize($myDirectory .'/' . $entryName));
Robbert
  • 6,481
  • 5
  • 35
  • 61
  • okay I got it to work but I had to modify that a little. After you tole me I needed to include the directory AND the file it made since but now it's telling me filesize() [function.filesize]: stat failed for..... file any file over 2 gigs. Looking at my code there is no max on there right so why would it error out on those files? – Joe Salmi Jan 29 '14 at 03:34
  • From the php documentation: Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB. http://www.php.net/manual/en/function.filesize.php – Robbert Jan 29 '14 at 03:43
  • Is there a way to change that? or what can I add to ignore the files larger than 2gb? – Joe Salmi Jan 29 '14 at 03:50
  • That was discussed here: http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program – Robbert Jan 29 '14 at 03:57