Php Disk Space Usage

Status
Not open for further replies.

javajenius

New Member
Messages
258
Reaction score
0
Points
0
Offer: 150 credits.

I need a php script that shows disk space usage, similar to the one cPanel uses. It should have graphics, and does not need to be too complex.

Thanks.
 
Last edited:

javajenius

New Member
Messages
258
Reaction score
0
Points
0
Actually i already had this, but i do not like it. I would like a bar instead of a pie chart, and a little more information. Thanks for your help anyway, is anyone else going to try this?
 

javajenius

New Member
Messages
258
Reaction score
0
Points
0
Ask Steck he will know how to do it... :D... Visit his site he has many tutorials wich could help.

I think he only does flash or photoshop. I saw his site and it has nothing to do with "php"

how will the max usage be inputted? database? file
File for now
 
Last edited:

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
well im attepmting to write something, but the disk_free_space and disk_total_space read the entire drive... as in the ENTIRE PARTITION... /home/

stoli has 200 gigs to /home/

or atleast i think...

idk where cpanel pulls its numbers from

EDIT: here is my test
http://partyvan.gotgrapedrink.com/bar/test.php
 
Last edited:

javajenius

New Member
Messages
258
Reaction score
0
Points
0
It is ok, but i would like the ability to chose a directory, and then read all the subdiretories and files under it, and get their length and then post the information. Not just read the disk, although it is useful.


loop through diretories and subdiretories, get length of the files with this:
Code:
  <?php
// example of how to get length filesize();

$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
 

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
Ok, done, I got it. Here is my example. Currently it is set to 1gig total space, and it tells me how much space is taken up by my entire directory. The folder that you choose and the given Total space available are 2 variables

http://partyvan.gotgrapedrink.com/bar/test.php

tell me what u think?
it does all subdirectories and files.
 
Last edited:

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
Wow!
Although it is simple, it is way better than anything I could do!
I'd give it 9/10.
:)
 

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
I'll have to show you the code sometime. I had to incorporate so much stuff! I can make the folder and max size in a seperate file with a require line... I'm totally a noob PHP coder, but i have many years in programming so i have a good grasp on syntax and things of that sort. I can pick up on other languages pretty quickly.

EDIT: I added a form to the test so you can change the max size on my demo. its in KB so 100,000 ~ 100MB. My folder is 100 something megs. enjoi the demo.

More: I HAVE SERIOUSLY LEARNED SO MUCH ABOUT PHP DUIN THIS! I THINK IM READY TO TAKE ON MORE TASKS
 
Last edited:

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
I agree. Saying, "Even though I'm a complete noob with this, I am going to get ______ done."
That's how I learned HTML. I am learning PHP now.
:)
I hope you post the code for us all to see.
 

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
Sure, hopefully since I should earn the point by now.
http://partyvan.gotgrapedrink.com/bar/test.php
CODE!!!!:::
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<!-- THIS STYLE PERTAINS TO THE BAR GRAPH IMAGES... ITS CSS, ONE OVER THE OTHER -->
<style type="text/css">
img.percentImage {
 background: white url(percentImage_back.png) top left no-repeat;
 padding: 0;
 margin: 5px 0 0 0;
 background-position: 1px 0;
}
</style>
</head>
<body>
<?php 
define ("DISK","/home/YOURDIRECTORY/"); //DIRECTORY WITH PERMISSIONS

if (!($_POST["maxvalue"])) //JUST AN IF FOR THE FORM INPUT
{
 define ("SPACE","1048576"); //IN KB MAX SIZE ( USE ONLY THIS LINE FOR REAL PURPOSES )
}
 else
{
 if (is_numeric ($_POST["maxvalue"]))
 {
  define ("SPACE",$_POST["maxvalue"]);
 }
 else
 {
  define ("SPACE","1048576"); //IN KB MAX SIZE
  echo "<script>alert('Try to use numbers');</script>"; //POPUP FOR TEXT INPUTS
 }
}
  function CountDir($aDir, $aRecurse) //HUGE LOOPS TO RUN THROUGH FOLDERS AND FILES
  { 
    $d = dir($aDir); 

    while ($Entry = $d->Read()) 
    { 
      if (!(($Entry == "..") || ($Entry == "."))) 
      { 
        if (Is_Dir($aDir . '/' . $Entry)) 
        { 
          if ($aRecurse) 
          { 
            $FinalValue += CountDir($aDir . '/' . $Entry, $aRecurse); 
          } 
        } 
        else 
        { 
          $FinalValue += round((filesize($aDir . '/' . $Entry))/1024, 2); //WHERE THE REAL MATH GOES DOWN
        } 
      } 
    } 
    return $FinalValue; 
  } 
 
$du = CountDir(DISK, True); 
//$df = round(disk_free_space(DISK)/1024/1024, 2); //OLD METHOD FOR WHOLE PARTITION
//$dt = round(disk_total_space(DISK)/1024/1024, 2);
$dt = SPACE;
$df = $dt-$du;
$percf = $df/$dt*100;
$percu = $du/$dt*100;
$outp = round(($percf/100*122)+1, 2); //SOME COOL MATH, 122 because its the size of the bar image
?>
<img src="percentImage.png" alt="<? echo round("$percu", 2); ?>%" class="percentImage" style="background-position: -<? echo "$outp"; ?>px 0pt;"/><!-- inputing the position of the bar and also the ALT test as the percent -->
<!-- ALL DEBUGS, USE HOW YOU LIKE -->
<br /><? echo round("$percu", 2); ?>% disk perc used
<br /><? echo round("$du"/1024, 2); ?> disk used megs
<br /><? echo round("$percf", 2); ?>% disk perc free
<br /><? echo round("$df"/1024, 2); ?> disk free megs
<br /><? echo round("$dt"/1024, 2); ?> disk total megs
<br /><br />
<form method="post" action="test.php"><!-- My form for value change -->
Change Max Space:<br />
<input type="text" name="maxvalue">KB
<br /><input type="submit" value="Change It!">
</form>
</body>
</html>
<!-- CREDS NOTHINGNESS, PM FOR CUSTOMIZATIONS -->
 
Last edited:

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Nice.

A couple of thing I've noticed:
1. You haven't closed you comments properly. ( <!-- Comment --> ) You had an "!" in the closing tags.
2. You didn't put a </FORM> tag anywhere.

These are just minor errors, nothing to really worry about, I just thought I'd tell you about them.
 

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
Thanks, if I use that on my site (I'm sure I will, it is awesome!), I'll give you some credits.
Is it also possible for you to post the code for test2.php? I am a real noob at PHP and like to learn from examples.
:)
 

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
Sure! Its a total hack-job on a snippet that I found, so I dont get all the credit in the end. Was fun learning though. Here...
Code:
<?php 
  function CountDir($aDir, $aRecurse) 
  { 
    $d = dir($aDir); 

    while ($Entry = $d->Read()) 
    { 
      if (!(($Entry == "..") || ($Entry == "."))) 
      { 
        if (Is_Dir($aDir . '/' . $Entry)) 
        { 
          if ($aRecurse) 
          { 
            $FinalValue += CountDir($aDir . '/' . $Entry, $aRecurse); 
          } 
        } 
        else 
        { 
          $FinalValue += round((filesize($aDir . '/' . $Entry))/1024, 2); //you can add another divide 1024 to make it megs, same with gigs
        } 
      } 
    } 
    return $FinalValue; 
  } 
 
echo CountDir("/home/YOURDIRECTORY/", True); 
echo " KB total";
?>

Also, if anyone chooses to use the code, for the bargraph, since its not GD, you need to the 2 images. I'll link em so they can be downloaded.

http://partyvan.gotgrapedrink.com/bar/percentImage.png
http://partyvan.gotgrapedrink.com/bar/percentImage_back.png
 
Last edited:
Status
Not open for further replies.
Top