Php Disk Space Usage

Status
Not open for further replies.

supajason

Member
Messages
288
Reaction score
2
Points
18
check this out http://jason.x10hosting.com/disk.php

PHP:
<?PHP

function directory_size($directory)
{
	$directorySize = 0;

	if($dh = @opendir($directory))
	{
		while (($filename = readdir ($dh)))
		{
			if($filename != "." && $filename != "..")
			{
				if(is_file($directory."/".$filename))
				{
					$directorySize += filesize($directory."/".$filename);
				}
				if(is_dir($directory."/".$filename))
				{
					$directorySize += directory_size($directory."/".$filename);
				}
			}
		}
	}
@closedir($dh);
return $directorySize;
}

$directory = "/home/jasoned/";
$maxByte = 314572800;
$dirByte = directory_size($directory);
$totalSize = round(($dirByte / 1048576), 2);
$maxSize = round(($maxByte / 1048576), 2);
$per = round(($totalSize / $maxSize) * 100);
echo "Directory : ";
echo $directory;
echo "<br>";
echo "Maximum Disk Space : ";
echo $maxSize;
echo "MB";
echo "<br>";
echo "Total Size : ";
echo $totalSize;
echo "MB";
echo "<br>";
echo "Percent : ";
echo $per;
echo "%";


if ($per <= "25") {
    $bg = "00FF55";
} elseif ($per <= "50") {
    $bg = "FFFF00";
} elseif ($per <= "100") {
    $bg = "FF0000";
}


?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test</title>
</head>
<body>

<table width="300" height="25" border="1" align="left" cellpadding="0" cellspacing="0" bordercolor="#00000">
  <tr>
    <td width="<?PHP echo $per; ?>%" bgcolor="#<?PHP echo $bg; ?>">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="25" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><?PHP echo $totalSize; ?>/<?PHP echo $maxSize; ?>MB</td>
  </tr>
</table>

</body>
</html>
 

kkenny

Active Member
Messages
1,950
Reaction score
0
Points
36
Pretty neat. Now if you could find one which shows your mySQL database size (everyone assumes it's the amount of space your hosting plan has, but i think not)

Anyways good job *rep*
 

DeadBattery

Community Support Team
Community Support
Messages
4,018
Reaction score
120
Points
0
Nice!
Did you code that yourself?
I think all of these are great. *adding to own website now*
:)
 

supajason

Member
Messages
288
Reaction score
2
Points
18
kkenny i will looking to the database size and let post back.
aopsftw yeah!

database size:

PHP:
<?php
$dbc = mysql_connect("hostname", "username","password");
mysql_select_db("dbname",$db);
$sql = "SHOW TABLE STATUS";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$totalSize = $row['Data_length']+$row['Index_length'];
}
echo($totalSize);
?>

this loops through the tables in the database and adds up the data length(in bytes)
 
Last edited:

javajenius

New Member
Messages
258
Reaction score
0
Points
0
I think i wrote this a while back,
ability to chose a directory
but thanks for the file size limit.

If you can get the above done, thats great, but i will give 100 credits each anyway.

EDIT: I gave credits, thanks for the help.

Nothingness your script does not work correctly on my site:
Warning: dir(/home/mirush12/perl) [function.dir]: failed to open dir: Permission denied in /home/mirush12/public_html/panel/disk2.php on line 38
I changed the permissions to 0777, i dont know why it still says this?


Supajason, i changed your code a little:
Code:
<?php
function directory_size($directory)
{
    $directorySize = 0;

    if($dh = @opendir($directory))
    {
        while (($filename = readdir ($dh)))
        {
            if($filename != "." && $filename != "..")
            {
                if(is_file($directory."/".$filename))
                {
                    $directorySize += filesize($directory."/".$filename);
                }
                if(is_dir($directory."/".$filename))
                {
                    $directorySize += directory_size($directory."/".$filename);
                }
            }
        }
    }
@closedir($dh);
return $directorySize;
}

$directory = "/home/mirush12/";
$maxByte = 1048576 * 1000;
$dirByte = directory_size($directory);
$totalSize = round(($dirByte / 1048576), 2);
$maxSize = round(($maxByte / 1048576), 2);
$per = round(($totalSize / $maxSize) * 100);
echo "Directory : ";
echo $directory;
echo "<br>";
echo "Maximum Disk Space : ";
echo $maxSize;
echo "MB";
echo "<br>";
echo "Total Size : ";
echo $totalSize;
echo "MB";
echo "<br>";
echo "Percent : ";
echo $per;
echo "%";


if ($per <= "25") {
    $bg = "00FF55";
} elseif ($per <= "50") {
    $bg = "FFFF00";
} elseif ($per <= "100") {
    $bg = "FF0000";
} 


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test</title>
</head>
<body>
<STYLE type=text/css>
td 
{
border-collapse: collapse; 
border: .05em solid #CCCCFF;
border-top: .05em solid #CCCCFF;
}
</STYLE>

<?php $f = 100-$per;?>
<table width="300" height="25" align="left" cellspacing="0" bordercolor="#00000">
  <tr>
    <td width="<?PHP echo $per; ?>%" bgcolor="#<?PHP echo $bg; ?>"><FONT FACE="Comic Sans MS" SIZE=4 COLOR="#CCCCFF"><?PHP if($per > 50){echo "<center>".$totalSize."/".$maxSize."MB"."</center>";} ?></FONT></td> 
    
<td width="<?php echo $f; ?>%" bgcolor="CCCCFF"><?PHP if($per < 50){
echo "<FONT FACE=\"Comic Sans MS\" SIZE=4><center>".$totalSize."/".$maxSize."MB"."</font></center>";} ?></td>
  </tr>
</table>
</body>
</html>

Demo here, log in as demo and demo, then click disk usage.
 
Last edited:

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
That might be a protected folder (/home/WATEVAR/perl) according to cpanel settings and things of that sort, espically since its not viewable to the public. Try any folder in the public_html to make sure its working.

EDIT: Thats really strange, I take u made that folder ur self, since i dont have one. Im testing some of the other folders. very weird.

PS: supajason: like it alot! didnt think of that, but yea, tables can also be used as very simple, customizable bar graphs. love how it changes colors too.
 
Last edited:

supajason

Member
Messages
288
Reaction score
2
Points
18
"ability to chose a directory" would you want a form for the input directory or a query string like disk.php?dir=/home/xxxxxx you tell me i dont mind changing it.

Nothingness i liked you graphics it looked really smart (Y) was going to try and use that as well as colour change but instead took the easy route. good old simple tables.

i guess i will start looking at your phpanel and start coding more if you need it?
 
Last edited:

javajenius

New Member
Messages
258
Reaction score
0
Points
0
"ability to chose a directory" would you want a form for the input directory or a query string like disk?dir=/home/xxxxxx you tell me i dont mind changing it.
Yeah, just give the user the option to check any folder inside of /home/.

Nothingness i liked you graphics it looked really smart (Y) was going to try and use that as well as colour change but instead took the easy route. good old simple tables.
I changed the colors a bit, hope you dont mind.

i guess i will start looking at your phpanel and start coding more if you need it?
I'm out of credits ;).

Next topic is password protection. Basic Auth for Apache (htaccess).

I think i have some general code already, but i dont know how to create the files =(.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>PASSWORD PROTECT</title>
</head>
<body>
<div>
<p>Please type in the path that you wish to protect:<br /></p>
<form action="protect.php" method="get">
<p><input type="text" name="path"/></p>
<br />
<p>Please type in a name for your authentication<br /></p>
<p><input type="text" name="field"/></p>
<p><input type="submit" value="Protect it!" /></p>
</form>
</div>
</body>
</html>

Protect.php
Code:
<?php

$path = $_GET['path'];
$field = $_GET['field'];
echo $path;
echo $field;


if(empty($path))
{ echo "You must type in a path!";}

echo "The folder \"$path\" is now protected.";

touch($path.".htaccess"); 
$fp = fopen( $path.".htaccess", "w" ) ) or die ("Couldn't open file, sorry");

fwrite( $fp, "AuthType Basic\n" );
fwrite( $fp, "AuthName /"$field/"" );
fwrite( $fp, "AuthUserFile /home/user/passes" );   //no idea how to do this
fwrite( $fp, "require valid-use" );
echo "DONE!";



fclose( $fp );

?>
 

supajason

Member
Messages
288
Reaction score
2
Points
18
"Yeah, just give the user the option to check any folder inside of /home/." - ok

colours are colours i just wanted it to change!

i will so the auth tomorrow if you want some help(dont worry about credits its all good fun for me)its really late now so im off
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Here's the script I use to get the size of a database, it's very simple and can only do 1 database, but it works.

PHP:
<?php
############### Settings ################
$hostname = "localhost";				// Name of server (Normally localhost)
$database = "database";	// Database Name
$username = "username";			// User with access to database
$password = "password";					// The password for the above user

############### The Script ##############

// formatfilesize function by Tim Bennett (http://www.drquincy.com/resources/code/php/formatfilesize)
function formatfilesize($data) {
	//bytes
	if($data<1024) {
			return $data."bytes";
		}
		
	//kilobytes
	else if($data<1024000) {
			return round(($data/1024),1)."k";
		}
		
	//megabytes
	else {
			return round(($data/1024000),1)."MB";
		}
}

mysql_connect($hostname,$username,$password);
mysql_select_db($database);
$result=mysql_query("SHOW TABLE STATUS");
$dbsize=0;
while($row=mysql_fetch_array($result))
{
$dbsize+=$row["Data_length"]+$row["Index_length"];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>Database Size</TITLE>
</HEAD>
<BODY>
<H2>Database Size</H2>
<P>The database <? echo $database; ?> is <? echo formatfilesize($dbsize); ?></P>
</BODY>
</HTML>
 
Last edited:

Nothingness

New Member
Messages
38
Reaction score
0
Points
0
TechAsh: in your code, a megabyte is 1048576, or 1024*1024. Just something I noticed.

Java: Customize it to your hearts content!
 

javajenius

New Member
Messages
258
Reaction score
0
Points
0
Nothingness, can you put all of your files for the disk usage in a zip and attach it to a post. Thank you.
 
Status
Not open for further replies.
Top