Question

I want to check the free space left on my server (using hosting service). However, I don't want to know the size of a whole disc, that my account accidentally is on. I only want to know, that I have used 456 MB from 1 GB of bought space.

I have tried to use code from this answer (Hosting Account Free Space), let me cite it:

$free = disk_free_space("/");
$total = disk_total_space("/");
$percent = ($free/$total) * 100;
echo $percent;

There is a problem, however. At first it give me a whole free space on the real device. I have 1GB space on my hosting, and this solution tells me that I have 202 GB free. BTW, I don't really understand why this question was closed. Maybe it was not clear, but the problem exists and the accepted answer is a little wrong in my opinion.

I have in mind a solution that is using exec() and bash function, but the question is: Is this possible using only PHP, without using exec()? exec() is sometimes blocked, and should be avoided due to code verification problems. Of course, for example mysqldump() should be run using exec(), but this is one of few exceptions.

Possible idea: I though about summing up all the files that are on my server, and in some kind of config.php file enter the account space manually. This one solve the problem, but not in the "nice" way. Especially if someone buys 1 GB more, and forget about the config "constant".

Was it helpful?

Solution

If the web hosting is using cPanel then you can use the cPanel API to call the getFreeSpace() and getSpaceUsed() function.

function getFreeSpace()
{
    $freeSpace = $this->parseIndex('Disk space available', 'float');
    return ($freeSpace == 0) ? 'Unlimited' : floatval($freeSpace);
}

Just my 2 penny

cPanel API: "cPanel URL"

Update:

I came across the web and found this php script (extracted from a cms plugin). So this is the script that I have tested on my web hosting:

Disk space script

<?php



// SETTINGS - START

// PUT YOUR CPANEL HOSTING USERNAME HERE:
    $username = "username";

// PUT YOUR CPANEL HOSTING USERNAME HERE:
    $password = "password";

// MODIFY THIS PATH TO REFLECT YOUR DOMAIN, REPLACING "DOMAIN-NAME" AND "YOUR-CPANEL-USERNAME":
    $query ="http://cpanel.yoursite.com:2082/xml-api/cpanel?user=USERNAME&cpanel_xmlapi_module=StatsBar&cpanel_xmlapi_func=stat&display=diskusage";

// SETTINGS - END


    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_HEADER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);
    curl_close($curl); 

    $xml = simpleXML_load_string($result);

    $df = $xml->data[0]->_count; // used MB
    $ds = $xml->data[0]->_max; // max MB
    $du = $ds - $df; // free MB
    if ($ds > 0) $perc = number_format(100 * $du / $ds, 2); else $perc = 0;
    $color = '#e87d7d';
    if ($perc > 50) $color = '#e8cf7d';
    if ($perc > 70) $color = '#ace97c';
    echo '<li style="font-weight:bold;padding:5px 15px;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;background-color:#182227;margin-left:13px;color:#afc5cf;">'
        .'Free disk space'
        .'<div style="border:1px solid #ccc;width:100%;margin:2px 5px 2px 0;padding:1px">'
        .'<div style="width:'.$perc.'%;background-color:'.$color.';height:6px"></div></div>'
        .$du.' of '.$ds.' MB free'.'</li>';

?>

Source script: http://get-simple.info/extend/plugin/free-disk-space-cpanel/317/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top