Click to See Complete Forum and Search --> : help generate filesize for each file in directory


indee
07-13-2006, 08:30 PM
since my host does not allow automatic directory listings like this http://forum.joomla.org/Themes/joomla/images/ (just googled any parent directory) i had to come up with one so i can easily download files from the directory. i found this script on planetsourcode.com and edited it a bit to make it work on my own site.

also i have a upload script which i use to upload files to the directory on my site.

i'm having trouble with getting each file in the directory to correctly read the correct filesize. i.e the file listing and links work but the filesize of each file reads the same as the last modified file in the directory.

this file is index.php in my directory

<?
////////////////////////////////////////////////////////
//Coded by: Canivour////////////////////////////////////
//Coded on: 03.19.01////////////////////////////////////
//Coded while listening to: Slipknot - Wait and Bleed///
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//Reads a directory and displays a link to every file///
//in the directory (other directories not included)/////
//and gives some info on the file, NOTE: Info works/////
//best on Unix systems (that should be obvious ;) //////
//Happy Coding//////////////////////////////////////////
////////////////////////////////////////////////////////

//starting directory
$startDir = "/home/www/AAA/BBB/CCC";
//open directory
$openDir = opendir($startDir);
//variable that holds the number of free bytes in the directory
$freeSpace = diskfreespace($startDir);
print "<center><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\" color=\"#000000\"> ";
print "Current Directory: ".$startDir."<br>"; //Current Directory
print "Free space in directory ".$startDir.": ".$freeSpace." bytes\n"; //Free space
print "<br>";
print "<hr noshade></font></center>";
print "<table border=0 cellspacing=1 cellpadding=4>\n<tr>\n<td>\n"; //starting of table
print "<b><font size=\"+2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Files</font></b>";
print "<br>";
print "<br>";
print "<table bgcolor=#000000 cellspacing=1 cellpadding=4>\n"; //another table
//loop while there are still things to be read in directory
while($path = readdir($openDir))
{
//gets the base name of file i.e instead of /home/canivour/html/index.html its index.html
$file = basename($path);
//makes sure we dont we read the . and .. direcotry (current directory and parent directory)
if($file!="." && $file!="..")
{
//if its not a directory print out the stats (can be changed for your needs)
if(!is_dir($startDir."/".$file))
{
$fullDir = $startDir."/".$file; //full directory path of file
$statCheck = stat($fullDir); //keeps info on files
print "<tr bgcolor=#ffffff><td width=500 height=20>\n";
print "<font size=\"-2\" face=\"Verdana\"><a href=".$file.">".$file."</a></font>\n"; //link to name of file
print "<font size=\"-2\" face=\"Verdana\">(".$statCheck[7]."B)</font>\n"; //link to name of file
print "</td></tr>\n";
} //end if
} //end if
} //end while
?>


at the moment it looks like this when viewd in the browser:

<html>
<head>
<title>Index of XXX</title>
<style>
<!--
a:link { text-decoration: none; color:#000000}
a:visited { color: #FF0000; text-decoration: line-through}
a:active { color: #000000; text-decoration: none}
a:hover { color: #000000; text-decoration: underline}
-->
</style>
</head>
<center><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="#000000"> Current Directory: /home/www/AAA/BBB/CCC<br>Free space in directory /home/www/AAA/BBB/CCC: 705623380 bytes
<br><hr noshade></font></center><table border=0 cellspacing=1 cellpadding=4>
<tr>
<td>
<b><font size="+2" face="Verdana, Arial, Helvetica, sans-serif">Files</font></b><br><br><table bgcolor=#000000 cellspacing=1 cellpadding=4>
<tr bgcolor=#ffffff><td width=500 height=20>
<font size="-2" face="Verdana"><a href=index.php>index.php</a></font>

<font size="-2" face="Verdana">(4096B)
</td></tr>
<tr bgcolor=#ffffff><td width=500 height=20>
<font size="-2" face="Verdana"><a href=test.txt>test.txt</a></font>
<font size="-2" face="Verdana">(4096B)
</td></tr>
<tr bgcolor=#ffffff><td width=500 height=20>
<font size="-2" face="Verdana"><a href=test02.txt>test02.txt</a></font>
<font size="-2" face="Verdana">(4096B)
</td></tr>
</html>


any help would be much appreciated

NogDog
07-13-2006, 09:08 PM
Use $statCheck[7] instead of the 11th element (which is the filesystem block-size, not the file size).

NogDog
07-13-2006, 09:10 PM
PS: looks like you need to close your <font> tag after you output the file size. (Or, better yet, since <font> is deprecated, look into using CSS styling.)

indee
07-14-2006, 03:13 AM
wow thanks

by the way is there any way to change it from showing in bytes to kilobytes?

pcthug
07-14-2006, 04:19 AM
This function will convert your bytes to kilobyte values:

function convertToKb($bytes)
{
$kbytes = $bytes / 1024;
$kbytes = number_format($kbytes, 2);
return $kbytes;
}

bokeh
07-14-2006, 04:47 AM
This function will convert your bytes to kilobyte values:

function convertToKb($bytes)
{
$kbytes = $bytes / 1024;
$kbytes = number_format($kbytes, 2);
return $kbytes;
}
And mega bytes:function megabytes($bytes)
{
return convertToKb(convertToKb($bytes));
}

indee
07-14-2006, 05:45 PM
cool, so how would i put that code into the original code above?

edit: heres the almost finished code, i just need to add the bytes to kilobytes code into but i don't know how. i think i might need to adjust the column percentages to make it look better.

this code should work for people who's host does't allow the, 'index of' pages


<html>
<head>
<title>Index of http://yourdomain.com/directory/list</title>
<style>
<!--
a:link { text-decoration: none; color:#000000}
a:visited { color: #FF0000; text-decoration: line-through}
a:active { color: #000000; text-decoration: none}
a:hover { color: #000000; text-decoration: underline}
.norm {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-color: #000000}
.thd { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #FFFFFF; font-weight: bold; }
-->
</style>
</head>
<?
////////////////////////////////////////////////////////
//Coded by: Canivour///////////////(19-03-2001)/////////
//Edited by: Indee/////////////////(15-07-2006)/////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//Reads a directory and displays a link to every file///
//in the directory (other directories not included)/////
//and gives some info on the file, NOTE: Info works/////
//best on Unix systems (that should be obvious ;) //////
////////////////////////////////////////////////////////
//If you choose to use this code this copyright/////////
//notice must be retained. Thanks, otherwise feel///////
//free to modify the code to suit your needs!///////////
////////////////////////////////////////////////////////

//starting directory
$startDir = "/home/www/yourdomain.com/directory/list";
//open directory
$openDir = opendir($startDir);
//variable that holds the number of free bytes in the directory
$freeSpace = diskfreespace($startDir);
print "<center><span class=\"norm\"> ";
print "Current Directory: ".$startDir."<br>"; //current directory path
print "Free space in directory ".$startDir.": ".$freeSpace." bytes\n"; //free space in directory
print "<br>";
print "</span><hr noshade color=#000000></center>";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n<tr>\n<td width=100%>\n"; //starting of table
print "<b><span class=\"norm\">Index of http://yourdomain.com/directory/list</span></b>";
print "<br>";
print "<br>";
//table headings
print "<table bgcolor=#000000 cellspacing=1 cellpadding=4 width=100%>\n"; //header table (main one to edit)**
print "<tr bgcolor=#000000><td width=65% height=20>\n";
print "<span class=\"thd\">Filename</span>\n"; //table head 'filename'
print "<td width=10% height=20><span class=\"thd\">Size (bytes)</span>\n"; //table head 'size' of the file
print "<td width=25% height=20><span class=\"thd\">Last Modified</span>\n"; //table head 'last modified' (unix timestamp)
print "</td></tr>\n";
//separate
print "<table bgcolor=#000000 cellspacing=1 cellpadding=4 width=100%>\n"; //file table (main one to edit)**
//loop while there are still things to be read in directory
while($path = readdir($openDir))
{
//gets the base name of file i.e instead of /home/canivour/html/index.html its index.html
$file = basename($path);
//makes sure we dont we read the . and .. direcotry (current directory and parent directory)
if($file!="." && $file!="..")
{
//if its not a directory print out the stats (can be changed for your needs)
if(!is_dir($startDir."/".$file))
{
$fullDir = $startDir."/".$file; //full directory path of file
$statCheck = stat($fullDir); //keeps info on files
print "<tr bgcolor=#ffffff><td width=65% height=20>\n";
print "<span class=\"norm\"><a href=".$file.">".$file."</a></span>\n"; //name of file incl link
print "<td width=10% height=20><span class=\"norm\">".$statCheck[7]."</span>\n"; //size of the file
print "<td width=25% height=20><span class=\"norm\">".$statCheck[9]."</span>\n"; //date last modified (unix timestamp)
print "</td></tr>\n";
} //end if
} //end if
} //end while
?>
</html>


optional things which could be nice
- if somebody knows the code to show what each of the filetypes are, or better still have icons for the different types people might have in a directory.
- sorting by filename, size, last modfied etc descending or ascending.
- login script to restrict access (on/off feature)

indee
11-17-2006, 09:31 PM
bump ^