Forum Moderators: coopster

Message Too Old, No Replies

opendir filesize convert from bytes to kb, mb, gb

opendir filesize convert from bytes to kb, mb, gb

         

drooh

6:44 am on Mar 1, 2007 (gmt 0)

10+ Year Member



i have a script that is opening a directory, reading the files and listing them out by name and filesize, my question is.

*is there a way to convert the bytes to kb, mb or gb? Is there a way to add commas?

i have
$fs = filesize($file);
echo $fs;

what else would i add to make it output something like this

6,332 bytes or
6.3 kb or others?

phranque

7:46 am on Mar 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



there is a solution on the php.net filesize manual page [us3.php.net].

coopster

4:08 pm on Mar 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Here is a function I use. Feel free to modify as you see necessary ...
function fileSizeInfo($fs) 
{
$bytes = array('KB', 'KB', 'MB', 'GB', 'TB');
// values are always displayed in at least 1 kilobyte:
if ($fs <= 999) {
$fs = 1;
}
for ($i = 0; $fs > 999; $i++) {
$fs /= 1024;
}
return array(ceil($fs), $bytes[$i]);
}

drooh

5:54 am on Mar 2, 2007 (gmt 0)

10+ Year Member



I am having a little difficulty integrating your script, here is what I have, maybe you can help me see whats wrong.

<?php
$dir = opendir(".");

while (($file = readdir($dir))!== false)
if ($file!= "." && $file!= "..")
{
$fs = filesize($file);

function fileSizeInfo($fs) { $bytes = array('KB', 'KB', 'MB', 'GB', 'TB');
// values are always displayed in at least 1 kilobyte:
if ($fs <= 999) { $fs = 1; }
for ($i = 0; $fs > 999; $i++) { $fs /= 1024; }
return array(ceil($fs), $bytes[$i]); }

echo "$file $fs";
}

closedir($dir);
?>

RonPK

8:41 am on Mar 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



drooh, you need to actually make a call to the function fileSizeInfo() to have it return something...

[edited by: RonPK at 8:42 am (utc) on Mar. 2, 2007]

coopster

12:36 pm on Mar 2, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Correct. Also, the function does not need to be inside your if/when control structures. Put it at the top of your code.

drooh

8:54 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



uhhh, i like to think of myself as someone who kinda knows this stuff, but what do you mean make a call? how do I do that? can you give me an example?

when i run the script it just says

Array

dreamcatcher

10:22 pm on Mar 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
function fileSizeInfo($fs) { $bytes = array('KB', 'KB', 'MB', 'GB', 'TB');
// values are always displayed in at least 1 kilobyte:
if ($fs <= 999) { $fs = 1; }
for ($i = 0; $fs > 999; $i++) { $fs /= 1024; }
return array(ceil($fs), $bytes[$i]); }

$dir = opendir(".");

while (($file = readdir($dir))!== false)
if ($file!= "." && $file!= "..")
{
$fs = filesize($file);

echo $file.' '.fileSizeInfo($fs);
}

closedir($dir);
?>

dc

drooh

7:33 pm on Mar 3, 2007 (gmt 0)

10+ Year Member



dreamcatcher, for some reason the code example you shows outputs like this

file001.mp3 array
file002.mp3 array
file003.mp3 array

dreamcatcher

7:46 pm on Mar 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I didn`t see the function returns an array. View the array as:

print_r(fileSizeInfo($fs));

dc

drooh

8:01 pm on Mar 3, 2007 (gmt 0)

10+ Year Member



here is a solution that seems to work,

<?php
function size_hum_read($size){
$i=0;
$iec = array("b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb");
while (($size/1024)>1) {
$size=$size/1024;
$i++;
}
return substr($size,0,strpos($size,'.')+4).$iec[$i];
}
$dir = opendir(".");
while (($file = readdir($dir))!== false)
if ($file!= "." && $file!= "..")
{
$fs = filesize($file);
//you can format this however you want
echo $file;
echo "<br>";
echo size_hum_read(filesize($file));
echo "<br>";
echo "<br>";
}
closedir($dir);
?>

drooh

10:24 pm on Mar 3, 2007 (gmt 0)

10+ Year Member



Here is a complete script system that allows you to list out everything that is in a directory, furthermore allowing users to view/link & direct download the files populated. It also shows the files size information in 2 different ways.

Save this 1st file as index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Directory</title>
<style type="text/css">
a:link {color: #FF0000; text-decoration: none}
a:visited {color: #00FF00; text-decoration: none}
a:hover {color: #FFFFFF; background-color: #FF0000; text-decoration: none}
a:active {color: #00FF00; text-decoration: none}
body {
color: black;
font: 12px verdana;
margin: 10px;
}
.filename {
color: black;
font-size: 14px;
}
.filesize {
color: blue;
}
.tier_1 {
background-color: #EFEFEF;
padding: 5px;
border: 1px solid;
margin-bottom: 5px;
}
.tier_2 {
background-color: #FFFFFF;
padding: 5px;
border: 1px solid;
margin-bottom: 5px;
}
</style>
</head>
<body>

<?php
function size_hum_read($size){
$i=0;
$iec = array("b", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb");
while (($size/1024)>1) {
$size=$size/1024;
$i++;
}
return substr($size,0,strpos($size,'.')+4).$iec[$i];
}
$dir = opendir(".");
while (($file = readdir($dir))!== false)
if ($file!= "." && $file!= ".." && $file!= "force-download.php" && $file!= "index.php")
{
$fs = filesize($file);
# style alternations
if ($alt == "1") {
$alt = "2";
}
else {
$alt = "1";
}
echo "<div class=\"tier_$alt\">\n";
echo "<span class=\"filename\">$file</span><br>\n";
echo "<span class=\"filesize\">",size_hum_read(filesize($file));
echo "&nbsp;[$fs bytes]</span><br>\n";
echo "<a href=\"force-download.php?file=$file\">[download]</a>&nbsp;&nbsp;<a href=\"$file\">[link]</a><br>\n";
echo "</div>\n\n";
}
closedir($dir);
?>
</body>
</html>

next save this file as force-download.php

<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
exit;
} elseif (! file_exists( $filename ) )
{
echo "<html><title>Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

finally put both of these files index.php and force-download.php in the server folder you want to populate a list for.

i found this script system usefull for sharing files to clients, for example a hip hop beat producer can put all his draft beats here and the remote emcee's can easily access them for writing their lyrics.

eelixduppy

1:54 am on Mar 5, 2007 (gmt 0)



Drooh, glad you found a solution. :)

If you still need help with functions, you should read up about them at php.net: Functions [us2.php.net].

Best of luck!