EvilZone

Programming and Scripting => Web Oriented Coding => : lucid May 22, 2013, 09:41:26 PM

: [PHP] File Size
: lucid May 22, 2013, 09:41:26 PM
This doesn't make any fucking sense. So I have this human readable filesize function:

: (php)
function read_size($bytes, $file)
{
    $kB = 1024;
    $MB = $kB * 1024;
    $GB = $MB * 1024;
    $TB = $GB * 1024;
   
    if(!is_file($file))
    {
        return "Directory";
    }
    elseif(is_file($file))
    {
        switch($bytes)
        {
            case $bytes < $kB:
                return $bytes . ' B';
                break;
            case $bytes >= $kB && $bytes < $MB:
                return round($bytes / $kB, 2) . ' KB';
                break;
            case $bytes >= $MB && $bytes < $GB:
                return round($bytes / $MB, 2) . ' MB';
                break;
            case $bytes >= $GB && $bytes < $TB:
                return round($bytes / $GB, 2) . ' GB';
                break;
            case $bytes >= $TB:
                return round($bytes / $TB, 2) . ' TB';
                break;
        }
    }
}

Also just to be thorough, here's where I call the function:

:
                <tr>
                    <td><?php echo $file?></td>
                    <td><?php echo read_size(filesize($path_and_file), $file); ?></td>
                    <td><?php echo view_perms($file); ?></td>

And at first glance it seems to work fine. It works just fine in the directory that it shows the contents of by default. However, when I back out to any other directory it says that everything is a directory. I just created a .txt file and in the filesize area it just said Directory instead of showing the filesize. I don't think that makes any fucking sense can you guys see a problem in my function?
: Re: [PHP] File Size
: ande May 22, 2013, 09:51:44 PM
There is nothing wrong. My best guess is that you are passing wrong file path values. If you send a path that does not exist, it will return Directory.
: Re: [PHP] File Size
: lucid May 22, 2013, 10:10:18 PM
Well, here's what the file manager looks like in the default directory

(http://i.imgur.com/ZKT7Fqw.png)

And here's what it looks like when I go back a directory

(http://i.imgur.com/qzE2KjU.png)

Hopefully that makes it clearer. I'm not sure what you mean by me sending a path that doesn't exist
: Re: [PHP] File Size
: ca0s May 22, 2013, 10:37:44 PM
Maybe
<?php echo read_size(filesize($path_and_file), $path_and_file); ?>
?
ande's right, you're probably passing that function a wrong path+file.

Btw, any special reason for doing this:
: (php)
    if(!is_file($file))
    {
    }
    elseif(is_file($file))
Instead of just
: (php)
    if(!is_file($file))
    {
    }
    else {
    }
?
: Re: [PHP] File Size
: lucid May 22, 2013, 10:45:58 PM
Well that was simpler than I imagined. You guys were right. All I did was change $file to $path_and_file and it works now. Thanks ande and ca0s.