Help with my code.
Posted: Fri Nov 01, 2013 9:44 am
Greetings
Can anyone assist me. I am trying to display current uploaded HD_space used by users on my website, but my PHP skills are lacking. I want it to brake down in to KB,MB,GB,TB. It is connecting to the database because when I print_r it shows Array ( [0] => Array ( [userID] => 26 [0] => 26 [SUM(fileSize)] => 3068815 [1] => 3068815 ) ) .
Any help greatly appreciated, I have spend hours trying to work this out.
Can anyone assist me. I am trying to display current uploaded HD_space used by users on my website, but my PHP skills are lacking. I want it to brake down in to KB,MB,GB,TB. It is connecting to the database because when I print_r it shows Array ( [0] => Array ( [userID] => 26 [0] => 26 [SUM(fileSize)] => 3068815 [1] => 3068815 ) ) .
Code: Select all
<?php
$sQL = "SELECT userID, SUM(fileSize) FROM file WHERE userId = ".$Auth->id." ";
$new = $db->getRows($sQL);
foreach($new[1] as $bytes);
/**
* Converts bytes into human readable file size.
*
* @param string $bytes
* @return string human readable file size (2,87 Мб)
* @author Mogilev Arseny
*/
function FileSizeConvert($bytes)
{
$bytes = floatval($bytes);
$arBytes = array(
0 => array(
"UNIT" => "TB",
"VALUE" => pow(1024, 4)
),
1 => array(
"UNIT" => "GB",
"VALUE" => pow(1024, 3)
),
2 => array(
"UNIT" => "MB",
"VALUE" => pow(1024, 2)
),
3 => array(
"UNIT" => "KB",
"VALUE" => 1024
),
4 => array(
"UNIT" => "B",
"VALUE" => 1
),
);
foreach($arBytes as $arItem)
{
if($bytes >= $arItem["VALUE"])
{
$result = $bytes / $arItem["VALUE"];
$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
echo $result;
print_r($new);
?>
Any help greatly appreciated, I have spend hours trying to work this out.