Forum Moderators: coopster
*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?
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]);
}
<?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);
?>
<?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
<?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);
?>
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 " [$fs bytes]</span><br>\n";
echo "<a href=\"force-download.php?file=$file\">[download]</a> <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.
If you still need help with functions, you should read up about them at php.net: Functions [us2.php.net].
Best of luck!