Related
I tried to make results like Facebook, but this doesn't work with my code, can you tell me where I did wrong?
sorry because I just learned php
This is my code :
<?php try {
$news_popular = "SELECT * FROM dUfA1_article ORDER BY id DESC";
$news_popular1 = $pdo->prepare($news_popular);
$news_popular1 ->execute(); }
catch (PDOException $exception) {
echo "ada kesalahan pada query : ".$exception->getMessage();
}
while ($data = $news_popular1->fetch(PDO::FETCH_ASSOC)) {
$ttl_likes = $data['likes'];
function number_format_short( $ttl, $precision = 1 ) {
if ($ttl < 900) {
$n_format = number_format($ttl, $precision);
$suffix = '';
} else if ($ttl < 900000) {
$n_format = number_format($ttl / 1000, $precision);
$suffix = 'K';
} else if ($ttl < 900000000) {
$n_format = number_format($ttl / 1000000, $precision);
$suffix = 'M';
} else if ($ttl < 900000000000) {
$n_format = number_format($ttl / 1000000000, $precision);
$suffix = 'B';
} else {
$n_format = number_format($ttl / 1000000000000, $precision);
$suffix = 'T';
}
if ( $precision > 0 ) {
$dotzero = '.' . str_repeat( '0', $precision );
$n_format = str_replace( $dotzero, '', $n_format );
}
return $n_format . $suffix;
}
$likes = number_format_short($ttl_likes);
echo " hasil = $likes <br>";
}?>
You can declare function with the same only once. Your number_format_short() declaration written inside the while loop so that if you while loop execute 5 times number_format_short() declare 5 times with the same.
Use below code
try {
$news_popular = "SELECT * FROM dUfA1_article ORDER BY id DESC";
$news_popular1 = $pdo->prepare($news_popular);
$news_popular1->execute();
} catch (PDOException $exception) {
echo "ada kesalahan pada query : " . $exception->getMessage();
}
while ($data = $news_popular1->fetch(PDO::FETCH_ASSOC)) {
$ttl_likes = $data['likes'];
$likes = number_format_short($ttl_likes);
echo " hasil = $likes <br>";
}
function number_format_short($ttl, $precision = 1) {
if ($ttl < 900) {
$n_format = number_format($ttl, $precision);
$suffix = '';
} else if ($ttl < 900000) {
$n_format = number_format($ttl / 1000, $precision);
$suffix = 'K';
} else if ($ttl < 900000000) {
$n_format = number_format($ttl / 1000000, $precision);
$suffix = 'M';
} else if ($ttl < 900000000000) {
$n_format = number_format($ttl / 1000000000, $precision);
$suffix = 'B';
} else {
$n_format = number_format($ttl / 1000000000000, $precision);
$suffix = 'T';
}
if ($precision > 0) {
$dotzero = '.' . str_repeat('0', $precision);
$n_format = str_replace($dotzero, '', $n_format);
}
return $n_format . $suffix;
}
?>
<?php
function number_format_short( $ttl, $precision = 1 ) {
if ($ttl < 900) {
$n_format = number_format($ttl, $precision);
$suffix = '';
} else if ($ttl < 900000) {
$n_format = number_format($ttl / 1000, $precision);
$suffix = 'K';
} else if ($ttl < 900000000) {
$n_format = number_format($ttl / 1000000, $precision);
$suffix = 'M';
} else if ($ttl < 900000000000) {
$n_format = number_format($ttl / 1000000000, $precision);
$suffix = 'B';
} else {
$n_format = number_format($ttl / 1000000000000, $precision);
$suffix = 'T';
}
if ( $precision > 0 ) {
$dotzero = '.' . str_repeat( '0', $precision );
$n_format = str_replace( $dotzero, '', $n_format );
}
return $n_format . $suffix;
}
try {
$news_popular = "SELECT * FROM dUfA1_article ORDER BY id DESC";
$news_popular1 = $pdo->prepare($news_popular);
$news_popular1->execute();
} catch (PDOException $exception) {
echo "ada kesalahan pada query : ".$exception->getMessage();
}
while ($data = $news_popular1->fetch(PDO::FETCH_ASSOC)) {
$ttl_likes = $data['likes'];
$likes = number_format_short($ttl_likes);
echo " hasil = $likes <br>";
}
?>
I'm new to CI but would like to include the code below (which sits in a file called color.php) within a view and reference the functions. But doesn't seem to work when I do this per normal php include include('/assets/color.php'); I've also tried adding color.php as a helper and loading it in the view but again not working … Any help on how best to include this would be great.
class GetMostCommonColors
{
var $PREVIEW_WIDTH = 150;
var $PREVIEW_HEIGHT = 150;
var $error;
/**
* Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the color.
*
* #return array
*/
function Get_Color( $img, $count=20, $reduce_brightness=true, $reduce_gradients=true, $delta=16 )
{
if (is_readable( $img ))
{
if ( $delta > 2 )
{
$half_delta = $delta / 2 - 1;
}
else
{
$half_delta = 0;
}
// WE HAVE TO RESIZE THE IMAGE, BECAUSE WE ONLY NEED THE MOST SIGNIFICANT COLORS.
$size = GetImageSize($img);
$scale = 1;
if ($size[0]>0)
$scale = min($this->PREVIEW_WIDTH/$size[0], $this->PREVIEW_HEIGHT/$size[1]);
if ($scale < 1)
{
$width = floor($scale*$size[0]);
$height = floor($scale*$size[1]);
}
else
{
$width = $size[0];
$height = $size[1];
}
$image_resized = imagecreatetruecolor($width, $height);
if ($size[2] == 1)
$image_orig = imagecreatefromgif($img);
if ($size[2] == 2)
$image_orig = imagecreatefromjpeg($img);
if ($size[2] == 3)
$image_orig = imagecreatefrompng($img);
// WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS
imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
$im = $image_resized;
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
$total_pixel_count = 0;
for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$total_pixel_count++;
$index = imagecolorat($im,$x,$y);
$colors = imagecolorsforindex($im,$index);
// ROUND THE COLORS, TO REDUCE THE NUMBER OF DUPLICATE COLORS
if ( $delta > 1 )
{
$colors['red'] = intval((($colors['red'])+$half_delta)/$delta)*$delta;
$colors['green'] = intval((($colors['green'])+$half_delta)/$delta)*$delta;
$colors['blue'] = intval((($colors['blue'])+$half_delta)/$delta)*$delta;
if ($colors['red'] >= 256)
{
$colors['red'] = 255;
}
if ($colors['green'] >= 256)
{
$colors['green'] = 255;
}
if ($colors['blue'] >= 256)
{
$colors['blue'] = 255;
}
}
$hex = substr("0".dechex($colors['red']),-2).substr("0".dechex($colors['green']),-2).substr("0".dechex($colors['blue']),-2);
if ( ! isset( $hexarray[$hex] ) )
{
$hexarray[$hex] = 1;
}
else
{
$hexarray[$hex]++;
}
}
}
// Reduce gradient colors
if ( $reduce_gradients )
{
// if you want to *eliminate* gradient variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$gradients = array();
foreach ($hexarray as $hex => $num)
{
if ( ! isset($gradients[$hex]) )
{
$new_hex = $this->_find_adjacent( $hex, $gradients, $delta );
$gradients[$hex] = $new_hex;
}
else
{
$new_hex = $gradients[$hex];
}
if ($hex != $new_hex)
{
$hexarray[$hex] = 0;
$hexarray[$new_hex] += $num;
}
}
}
// Reduce brightness variations
if ( $reduce_brightness )
{
// if you want to *eliminate* brightness variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$brightness = array();
foreach ($hexarray as $hex => $num)
{
if ( ! isset($brightness[$hex]) )
{
$new_hex = $this->_normalize( $hex, $brightness, $delta );
$brightness[$hex] = $new_hex;
}
else
{
$new_hex = $brightness[$hex];
}
if ($hex != $new_hex)
{
$hexarray[$hex] = 0;
$hexarray[$new_hex] += $num;
}
}
}
arsort( $hexarray, SORT_NUMERIC );
// convert counts to percentages
foreach ($hexarray as $key => $value)
{
$hexarray[$key] = (float)$value / $total_pixel_count;
}
if ( $count > 0 )
{
// only works in PHP5
// return array_slice( $hexarray, 0, $count, true );
$arr = array();
foreach ($hexarray as $key => $value)
{
if ($count == 0)
{
break;
}
$count--;
$arr[$key] = $value;
}
return $arr;
}
else
{
return $hexarray;
}
}
else
{
$this->error = "Image ".$img." does not exist or is unreadable";
return false;
}
}
function _normalize( $hex, $hexarray, $delta )
{
$lowest = 255;
$highest = 0;
$colors['red'] = hexdec( substr( $hex, 0, 2 ) );
$colors['green'] = hexdec( substr( $hex, 2, 2 ) );
$colors['blue'] = hexdec( substr( $hex, 4, 2 ) );
if ($colors['red'] < $lowest)
{
$lowest = $colors['red'];
}
if ($colors['green'] < $lowest )
{
$lowest = $colors['green'];
}
if ($colors['blue'] < $lowest )
{
$lowest = $colors['blue'];
}
if ($colors['red'] > $highest)
{
$highest = $colors['red'];
}
if ($colors['green'] > $highest )
{
$highest = $colors['green'];
}
if ($colors['blue'] > $highest )
{
$highest = $colors['blue'];
}
// Do not normalize white, black, or shades of grey unless low delta
if ( $lowest == $highest )
{
if ($delta <= 32)
{
if ( $lowest == 0 || $highest >= (255 - $delta) )
{
return $hex;
}
}
else
{
return $hex;
}
}
for (; $highest < 256; $lowest += $delta, $highest += $delta)
{
$new_hex = substr("0".dechex($colors['red'] - $lowest),-2).substr("0".dechex($colors['green'] - $lowest),-2).substr("0".dechex($colors['blue'] - $lowest),-2);
if ( isset( $hexarray[$new_hex] ) )
{
// same color, different brightness - use it instead
return $new_hex;
}
}
return $hex;
}
function _find_adjacent( $hex, $gradients, $delta )
{
$red = hexdec( substr( $hex, 0, 2 ) );
$green = hexdec( substr( $hex, 2, 2 ) );
$blue = hexdec( substr( $hex, 4, 2 ) );
if ($red > $delta)
{
$new_hex = substr("0".dechex($red - $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($green > $delta)
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green - $delta),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($blue > $delta)
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue - $delta),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($red < (255 - $delta))
{
$new_hex = substr("0".dechex($red + $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($green < (255 - $delta))
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green + $delta),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($blue < (255 - $delta))
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue + $delta),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
return $hex;
}
}
// loading library from within view
$this->load->library('GetMostCommonColors');
$ex = new GetMostCommonColors();
$colors=$ex->Get_Color($artwork->large_image, 5);
Put The file into library folder and load the the library using this code
$this->load->library('GetMostCommonColors');
This link will help you..
Library detail documentation
Seems the path is not correct, try the following code:
require_once(dirname(__FILE__).'/assets/color.php');
or
require_once __DIR__ . '/assets/color.php';
but
include('/assets/color.php');
will work as well provided the assets folder is a sub-folder of the folder where the main file is from where you are making this inclusion.
function humanFileSize($size)
{
if ($size >= 1073741824) {
$fileSize = round($size / 1024 / 1024 / 1024,1) . 'GB';
} elseif ($size >= 1048576) {
$fileSize = round($size / 1024 / 1024,1) . 'MB';
} elseif($size >= 1024) {
$fileSize = round($size / 1024,1) . 'KB';
} else {
$fileSize = $size . ' bytes';
}
return $fileSize;
}
... works great except: I can't manually choose in what format I need to display, say i want to show in MB only whatever the file size is. Currently if its in the GB range, it would only show in GB.
Also, how do I limit the decimal to 2?
Try something like this:
function humanFileSize($size,$unit="") {
if( (!$unit && $size >= 1<<30) || $unit == "GB")
return number_format($size/(1<<30),2)."GB";
if( (!$unit && $size >= 1<<20) || $unit == "MB")
return number_format($size/(1<<20),2)."MB";
if( (!$unit && $size >= 1<<10) || $unit == "KB")
return number_format($size/(1<<10),2)."KB";
return number_format($size)." bytes";
}
There is great example by Jeffrey Sambells:
function human_filesize($bytes, $dec = 2): string {
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor == 0) $dec = 0;
return sprintf("%.{$dec}f %s", $bytes / (1024 ** $factor), $size[$factor]);
}
echo human_filesize(filesize('example.zip'));
I'm using this method:
function byteConvert($bytes)
{
if ($bytes == 0)
return "0.00 B";
$s = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes, 1024));
return round($bytes/pow(1024, $e), 2).$s[$e];
}
works great in o(1).
A pretty short 3 lines method that I use (1024 = 1KB) and supports from KB to YB is the following one:
<?php
/**
* Converts a long string of bytes into a readable format e.g KB, MB, GB, TB, YB
*
* #param {Int} num The number of bytes.
*/
function readableBytes($bytes) {
$i = floor(log($bytes) / log(1024));
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
return sprintf('%.02F', $bytes / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
}
// "1000 B"
echo readableBytes(1000);
// "9.42 MB"
echo readableBytes(9874321);
// "9.31 GB"
// The number of bytes as a string is accepted as well
echo readableBytes("10000000000");
// "648.37 TB"
echo readableBytes(712893712304234);
// "5.52 PB"
echo readableBytes(6212893712323224);
More info about these methods on this article.
To expand on Vaidas' answer, here's how you should do it to account for the new IEC standards:
function human_readable_bytes($bytes, $decimals = 2, $system = 'binary')
{
$mod = ($system === 'binary') ? 1024 : 1000;
$units = array(
'binary' => array(
'B',
'KiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB',
),
'metric' => array(
'B',
'kB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB',
),
);
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f%s", $bytes / pow($mod, $factor), $units[$system][$factor]);
}
Technically, according to the specifications for storage devices and such you should use the metric system as default (that's why Google converter shows kB -> MB as mod 1000 instead of 1024).
Here's my custom function for displaying human readable file size:
function getHumanReadableSize($bytes) {
if ($bytes > 0) {
$base = floor(log($bytes) / log(1024));
$units = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"); //units of measurement
return number_format(($bytes / pow(1024, floor($base))), 3) . " $units[$base]";
} else return "0 bytes";
}
function bytesToHuman($bytes)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
return round($bytes, 2) . ' ' . $units[$i];
}
Credit: https://laracasts.com/discuss/channels/laravel/human-readable-file-size-and-time?page=1#reply=115796
You can modify your function to fullfil both your need to force a unit if given and adjust the precision.
function humanFileSize($size, $precision = 1, $show = "")
{
$b = $size;
$kb = round($size / 1024, $precision);
$mb = round($kb / 1024, $precision);
$gb = round($mb / 1024, $precision);
if($kb == 0 || $show == "B") {
return $b . " bytes";
} else if($mb == 0 || $show == "KB") {
return $kb . "KB";
} else if($gb == 0 || $show == "MB") {
return $mb . "MB";
} else {
return $gb . "GB";
}
}
//Test with different values
echo humanFileSize(1038) . "<br />";
echo humanFileSize(103053, 0) . "<br />";
echo humanFileSize(103053) . "<br />";
echo humanFileSize(1030544553) . "<br />";
echo humanFileSize(1030534053405, 2, "GB") . "<br />"; ;
function getHumanReadableSize($size, $unit = null, $decemals = 2) {
$byteUnits = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
if (!is_null($unit) && !in_array($unit, $byteUnits)) {
$unit = null;
}
$extent = 1;
foreach ($byteUnits as $rank) {
if ((is_null($unit) && ($size < $extent <<= 10)) || ($rank == $unit)) {
break;
}
}
return number_format($size / ($extent >> 10), $decemals) . $rank;
}
If php version below 5.4 use
$byteUnits = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
I wanted a function that returned filesizes like Windows does, and surprisingly I could find none at all. Even worse, some here and elsewhere are broken in that they assume 1KB = 1000B.
So I coded one! Plus two helper functions. Here they are:
// Returns a size in a human-readable form from a byte count.
function humanSize($bytes)
{
if ($bytes < 1024) return "$bytes Bytes";
$units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
foreach ($units as $i => $unit)
{
// The reason for this threshold is to avoid e.g., "1000 KB",
// instead jumping from e.g., "999 KB" to "0.97 MB".
$multiplier = pow(1024, $i + 1);
$threshold = $multiplier * 1000;
if ($bytes < $threshold)
{
$size = formatToMinimumDigits($bytes / $multiplier, false);
return "$size $unit";
}
}
}
// Efficiently calculates how many digits the integer portion of a number has.
function digits($number)
{
// Yes, I could convert to string and count the characters,
// but this is faster and cooler.
$log = log10($number);
if ($log < 0) return 1;
return floor($log) + 1;
}
// Formats a number to a minimum amount of digits.
// In other words, makes sure that a number has at least $digits on it, even if
// that means introducing redundant decimal zeroes at the end, or rounding the
// ones present exceeding the $digits count when combined with the integers.
// For example:
// formatToMinimumDigits(10) // 10.0
// formatToMinimumDigits(1.1) // 1.10
// formatToMinimumDigits(12.34) // 12.3
// formatToMinimumDigits(1.234) // 1.23
// formatToMinimumDigits(1.203) // 1.20
// formatToMinimumDigits(123.4) // 123
// formatToMinimumDigits(100) // 100
// formatToMinimumDigits(1000) // 1000
// formatToMinimumDigits(1) // 1.00
// formatToMinimumDigits(1.002) // 1.00
// formatToMinimumDigits(1.005) // 1.01
// formatToMinimumDigits(1.005, false) // 1.00
// This is primarily useful for generating human-friendly numbers.
function formatToMinimumDigits($value, $round = true, $digits = 3)
{
$integers = floor($value);
$decimalsNeeded = $digits - digits($integers);
if ($decimalsNeeded < 1)
{
return $integers;
}
else
{
if ($round)
{
// This relies on implicit type casting of float to string.
$parts = explode('.', round($value, $decimalsNeeded));
// We re-declare the integers because they may change
// after we round the number.
$integers = $parts[0];
}
else
{
// Again, implicit type cast to string.
$parts = explode('.', $value);
}
// And because of the implicit type cast, we must guard against
// 1.00 becoming 1, thus not exploding the second half of it.
$decimals = isset($parts[1]) ? $parts[1] : '0';
$joined = "$integers.$decimals".str_repeat('0', $digits);
return substr($joined, 0, $digits + 1);
}
}
Usage is as simple as humanSize(123456789).
This is how I use, it's clean and simple.
Also can be used like this:
public function getHumanReadableFilesize(int $bytes, int $dec = 2): string
{
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$dec}f %s", ($bytes / (1024 ** $factor)), $size[$factor]);
}
Thanks #Márton Tamás for suggestion to add like comment.
Here is a working function managing till Yottabyte:
function DisplayFileSize($size, $unit = false, $precision = 2){
$b = $size;
$kb = round($size / 1024, $precision);
$mb = round($kb / 1024, $precision);
$gb = round($mb / 1024, $precision);
$tb = round($gb / 1024, $precision);
$pb = round($tb / 1024, $precision);
$eb = round($pb / 1024, $precision);
$zb = round($eb / 1024, $precision);
$yb = round($zb / 1024, $precision);
if((!$unit && floor($kb) == 0) || $unit == "b") {
return array("value" => FormatNumber($b), "unit" => "bytes");
} else if((!$unit && floor($mb) == 0) || $unit == "kb") {
return array("value" => FormatNumber($kb, 2), "unit" => "Kb");
} else if((!$unit && floor($gb) == 0) || $unit == "mb") {
return array("value" => FormatNumber($mb, 2), "unit" => "Mb");
} else if((!$unit && floor($tb) == 0) || $unit == "gb") {
return array("value" => FormatNumber($gb, 2), "unit" => "Gb");
} else if((!$unit && floor($pb) == 0) || $unit == "tb") {
return array("value" => FormatNumber($tb, 2), "unit" => "Tb");
} else if((!$unit && floor($eb) == 0) || $unit == "pb") {
return array("value" => FormatNumber($pb, 2), "unit" => "Pb");
} else if((!$unit && floor($zb) == 0) || $unit == "eb") {
return array("value" => FormatNumber($eb, 2), "unit" => "Eb");
} else if((!$unit && floor($yb) == 0) || $unit == "zb") {
return array("value" => FormatNumber($zb, 2), "unit" => "Zb");
} else {
return array("value" => FormatNumber($yb, 2), "unit" => "Yb");
}
}
This question already has answers here:
Format bytes to kilobytes, megabytes, gigabytes
(28 answers)
Closed 3 years ago.
How can I convert the output of PHP's filesize() function to a nice format with MegaBytes, KiloBytes etc?
like:
if the size is less than 1 MB, show the size in KB
if it's between 1 MB - 1 GB show it in MB
if it's larger - in GB
Here is a sample:
<?php
// Snippet from PHP Share: http://www.phpshare.org
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
?>
Even nicer is this version I created from a plugin I found:
function filesize_formatted($path)
{
$size = filesize($path);
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
Note from filesize() doc
Because PHP's integer type is signed and many platforms use 32bit
integers, some filesystem functions may return unexpected results for
files which are larger than 2GB
A cleaner approach:
function Size($path)
{
$bytes = sprintf('%u', filesize($path));
if ($bytes > 0)
{
$unit = intval(log($bytes, 1024));
$units = array('B', 'KB', 'MB', 'GB');
if (array_key_exists($unit, $units) === true)
{
return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
}
}
return $bytes;
}
I think this is a better approach. Simple and straight forward.
public function sizeFilter( $bytes )
{
$label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ );
return( round( $bytes, 2 ) . " " . $label[$i] );
}
This is based on #adnan's great answer.
Changes:
added internal filesize() call
return early style
saving one concatentation on 1 byte
And you can still pull the filesize() call out of the function, in order to get a pure bytes formatting function. But this works on a file.
/**
* Formats filesize in human readable way.
*
* #param file $file
* #return string Formatted Filesize, e.g. "113.24 MB".
*/
function filesize_formatted($file)
{
$bytes = filesize($file);
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
return $bytes . ' bytes';
} elseif ($bytes == 1) {
return '1 byte';
} else {
return '0 bytes';
}
}
All the answers to the question uses that 1 kilobyte = 1024 bytes which is wrong! (1 kibibyte = 1024 bytes)
since the question asks to convert file sizes, it should use that 1 kilobyte = 1000 bytes (see https://wiki.ubuntu.com/UnitsPolicy)
function format_bytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1000));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1000, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
This would be a cleaner implementation:
function size2Byte($size) {
$units = array('KB', 'MB', 'GB', 'TB');
$currUnit = '';
while (count($units) > 0 && $size > 1024) {
$currUnit = array_shift($units);
$size /= 1024;
}
return ($size | 0) . $currUnit;
}
A complete example.
<?php
$units = explode(' ','B KB MB GB TB PB');
echo("<html><body>");
echo('file size: ' . format_size(filesize("example.txt")));
echo("</body></html>");
function format_size($size) {
$mod = 1024;
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
$endIndex = strpos($size, ".")+3;
return substr( $size, 0, $endIndex).' '.$units[$i];
}
?>
function calcSize($size,$accuracy=2) {
$units = array('b','Kb','Mb','Gb');
foreach($units as $n=>$u) {
$div = pow(1024,$n);
if($size > $div) $output = number_format($size/$div,$accuracy).$u;
}
return $output;
}
function getNiceFileSize($file, $digits = 2){
if (is_file($file)) {
$filePath = $file;
if (!realpath($filePath)) {
$filePath = $_SERVER["DOCUMENT_ROOT"] . $filePath;
}
$fileSize = filesize($filePath);
$sizes = array("TB", "GB", "MB", "KB", "B");
$total = count($sizes);
while ($total-- && $fileSize > 1024) {
$fileSize /= 1024;
}
return round($fileSize, $digits) . " " . $sizes[$total];
}
return false;
}
//Get the size in bytes
function calculateFileSize($size)
{
$sizes = ['B', 'KB', 'MB', 'GB'];
$count=0;
if ($size < 1024) {
return $size . " " . $sizes[$count];
} else{
while ($size>1024){
$size=round($size/1024,2);
$count++;
}
return $size . " " . $sizes[$count];
}
}
Im trying to format the output of numbers in php. I have an amount of posts that show up, and next to each user is the total of posts. But it shows that actual amount, i want it to show it in a shorter format, actually, just like they do here at SO with reputation
any ideas?
<?
$numbers = array(100,1000,15141,3421);
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k"; // NB: you will want to round this
}
else {
return $number;
}
}
foreach($numbers as $number) {
echo $number . " : " . format_number($number);
echo "\n";
}
function count_format($n, $point='.', $sep=',') {
if ($n < 0) {
return 0;
}
if ($n < 10000) {
return number_format($n, 0, $point, $sep);
}
$d = $n < 1000000 ? 1000 : 1000000;
$f = round($n / $d, 1);
return number_format($f, $f - intval($f) ? 1 : 0, $point, $sep) . ($d == 1000 ? 'k' : 'M');
}
Use This
Shorten long numbers to K/M/B?
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
// 0.9k-850k
$n_format = number_format($n / 1000, $precision);
$suffix = 'K';
} else if ($n < 900000000) {
// 0.9m-850m
$n_format = number_format($n / 1000000, $precision);
$suffix = 'M';
} else if ($n < 900000000000) {
// 0.9b-850b
$n_format = number_format($n / 1000000000, $precision);
$suffix = 'B';
} else {
// 0.9t+
$n_format = number_format($n / 1000000000000, $precision);
$suffix = 'T';
}