Reputation: 2232
I'm trying to get the size of my website using php and ftp. I would like to get the size of everything under httpdocs, (files and directories combined)
And have it output: My website is 42.5MB.
I've tried ftp_rawlist, but that gives me an ugly array that I have no idea how to parse. (Im new to php arrays). It also only gives me the file sizes of the files under httpdocs, not files within directories in httpdocs.
Is there some sort of recursive function that will do this?
Upvotes: 2
Views: 555
Reputation: 2232
Figured it out.
Anyway, here is what I wrote up to get this working.
<?php
$host = 'HOST';
$username = 'USERNAME';
$password = 'PASSWORD';
$dir_is = 'DIRECTORY_TO_USE';
$conn_id = ftp_connect($host);
$login_result = ftp_login($conn_id, $username,$password);
$rawfiles = ftp_rawlist($conn_id, $dir_is, true);
$total_size = 0;
foreach($rawfiles as $info){
$info = preg_split("/[\s]+/", $info, 9);
$size = $info[4];
$total_size += $size;
}
function convert_size($size, $decimals = 1) {
$suffix = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB','NB','DB');
$i = 0;
while ($size >= 1024 && ($i < count($suffix) - 1)){
$size /= 1024;
$i++;
}
return round($size, $decimals).' '.$suffix[$i];
}
echo 'Total Size is: '.convert_size($total_size);
?>
Replace with your connection info:
$host = 'HOST';
$username = 'USERNAME';
$password = 'PASSWORD';
$dir_is = 'DIRECTORY_TO_USE';
Upvotes: 2
Reputation: 29462
Is there some sort of recursive function that will do this?
No, you have to write it yourself. If you are new to arrays it means you are completely new to php, since most of work there is done on arrays. You would need more string parsing here and here is general algorithm:
This is a little walk-around version, proper would be to use sscanf to parse single line, but I think this one presented above is easier to understand for beginner
Upvotes: 0