FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Disk quota left on Frihost





Flakky
Is it possible to see using PHP how much disk quota is left on your account on Frihost?
I want to make an automatic delete function which only works if disk space is less than #mb.
imagefree
you can do it in the reverse way. Find the disk space used and then deduct it from the total Quota.


Code:
/**
 * Calculate the full size of a directory
 *
 * @author      Jonas John
 * @version     0.2
 * @link        http://www.jonasjohn.de/snippets/php/dir-size.htm
 * @param       string   $DirectoryPath    Directory path
 */
function CalcDirectorySize($DirectoryPath) {
 
    // I reccomend using a normalize_path function here
    // to make sure $DirectoryPath contains an ending slash
    // (-> http://www.jonasjohn.de/snippets/php/normalize-path.htm)
 
    // To display a good looking size you can use a readable_filesize
    // function.
    // (-> http://www.jonasjohn.de/snippets/php/readable-filesize.htm)
 
    $Size = 0;
 
    $Dir = opendir($DirectoryPath);
 
    if (!$Dir)
        return -1;
 
    while (($File = readdir($Dir)) !== false) {
 
        // Skip file pointers
        if ($File[0] == '.') continue;
 
        // Go recursive down, or add the file size
        if (is_dir($DirectoryPath . $File))           
            $Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
        else
            $Size += filesize($DirectoryPath . $File);       
    }
 
    closedir($Dir);
 
    return $Size;
}


There is an even better solution at: http://www.go4expert.com/forums/showthread.php?t=290
jmraker
If the files are less than 2 gig you can loop through all the files and directories and use a total of all the file sizes.

http://us3.php.net/manual/en/function.filesize.php
http://us3.php.net/manual/en/function.glob.php
http://us3.php.net/manual/en/function.is-dir.php
http://us3.php.net/manual/en/function.is-file.php

Where the code looks like
Code:
function du($dir){
        $total = 0;
   foreach(glob($dir . '*') as $file){
      if(is_dir($file)){
         $total += du($file . '/');
      }
      elseif(is_file($file)){
         $total += filesize($file);
      }
   }
        return $total;
}


If there are files over 2 gig you can have the program ftp into your site and use ftp commands to do the same things (recursively going through directories) and use
http://us3.php.net/manual/en/function.ftp-rawlist.php
for the "ls -l" info since the php program can't execute "ls -l" itself

(from a comment on php.net)
Quote:
<?php # copyright by fackelkind | codeMaster
function getRecDirSize ($connection, $dir){
$temp = ftp_rawlist ($connection, "-alR $dir");
foreach ($temp as $file){
if (ereg ("([-d][rwxst-]+).* ([0-9]) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file, $regs)){
$isdir = (substr ($regs[1],0,1) == "d");
if (!$isdir)
$size += $regs[5];
}
}
return $size;
}
$dirSize = getRecDirSize ($conID, "/");
?>
Flakky
Hi,

Thanks for the replies. I used the code that jmraker supplied but that gives me both the answer and an error (invalid argument supplied for foreach() )
Imagefree, that code gives me a lot of errors.
I thank you, I'll try to fix that error unless someone else knows how to solve this?

EDIT: I solved it by disabling error reporting and enabling it afterwards. Works fine Wink Lazy solution
jmraker
If you used glob() it must have returned false (an error) on one or more of the calls, just move the glob outside of the loop and test it to see if it's an array.

Code:
$files = glob(...);
if(is_array($files)){
  foreach($files as $file){
    ... rest of code  ...
  }
  return $total;
}
Flakky
Thanks jmraker, will try that Smile
Related topics
[RESOLVED] Disk Usage Warning (critical)
Disk Quota Exceded????
Uebimail displays incorrect quota information
account quota use
disk space
Socket error = #10053..Anyone?
Disk space needed
Increase the disk space
¿Es bueno Frihost?
Server 2/3 disk quota changed to 500MB
Update home page - 250 to 500 MB
More Options¬
Forums Slowed Down?
Nice to know the disk size is increase and bandwith!!!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.