This forum is in archive mode. You will not be able to post new content.

Author Topic: [PHP] File Size  (Read 819 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
[PHP] File Size
« on: May 22, 2013, 09:41:26 PM »
This doesn't make any fucking sense. So I have this human readable filesize function:

Code: (php) [Select]
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:

Code: [Select]
                <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?
« Last Edit: May 22, 2013, 09:43:58 PM by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: [PHP] File Size
« Reply #1 on: 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.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP] File Size
« Reply #2 on: May 22, 2013, 10:10:18 PM »
Well, here's what the file manager looks like in the default directory



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



Hopefully that makes it clearer. I'm not sure what you mean by me sending a path that doesn't exist
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [PHP] File Size
« Reply #3 on: 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:
Code: (php) [Select]
    if(!is_file($file))
    {
    }
    elseif(is_file($file))
Instead of just
Code: (php) [Select]
    if(!is_file($file))
    {
    }
    else {
    }
?

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP] File Size
« Reply #4 on: 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.
« Last Edit: May 22, 2013, 10:46:43 PM by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.