Jump to content

Trying to cobble together a directory listing with link and file sizes


pineypl

Recommended Posts

I am a noob here and have cobbled together some php coding from things I found on the web.  I am wanting to generate a table with the file name, which is linkable for download, file size in a human readable way, and then the file time.  I am tracking civil engineering plans for final construction and want the users to see when the files were made and to have an idea of what size they are prior to downloading them.

 

So far, it is working nicely, except I am not sharp enough to get the human readable size function to work with my array and it's subsequent sorting.  I have spent two days googling examples and such and I just can't get it.

 

So please swing away with the clue bat and show me how I am stupid and should not mess with this stuff.  ;) And thank you ahead of time for taking the time to look at this and hopefully respond.

 

Here is my code.

<?

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]; 
}

  // open this directory 
  $myDirectory = opendir(".");
         
  // get each entry
  while(false !== ($entryName = readdir($myDirectory))) {
  	if (strpos($entryName, '.pdf',1)||strpos($entryName, '.doc',1)||strpos($entryName, '.hash',1) )
  	$dirArray[] = $entryName;

$fs = filesize($dirArray[$index]);

}
  
  // close directory
  closedir($myDirectory);
  
  //	count elements in array
  $indexCount	= count($dirArray);
  
  // sort them
  sort($dirArray);
  
  // print them
  print("<TABLE border=1 cellpadding=5 cellspacing=0 width=98%>\n");
  print("<TR><TH colspan=3><A HREF=..>Up to Parent Folder</A></TH></TR>");
  print("<TR><TH>File name</TH><th>File size</th><th>File date</th></TR>\n");
  // loop through the array of files and print them all
  for($index=0; $index < $indexCount; $index++) {
//          if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
  		print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
  		print("<td>");
	print(filesize($dirArray[$index]));
  		print("</td>");
  		print("<td>");
  		print(date("F d Y H:i",filemtime($dirArray[$index])));
  		print("</td>");
  		print("</TR>\n");
  	}
  //}
print("</TABLE>\n");

?>

Well I'll start by making your code readable by humans ::)

 

<?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]; 
}

// open this directory 
$myDirectory = opendir(".");
         
// get each entry
while(false !== ($entryName = readdir($myDirectory))) {
    if(strpos($entryName, '.pdf', 1) || strpos($entryName, '.doc', 1) || strpos($entryName, '.hash', 1)) {
        $dirArray[] = $entryName;
    }
}
  
// close directory
closedir($myDirectory);
  
// count elements in array
$indexCount = count($dirArray);
  
// sort them
sort($dirArray);
  
// print them
print("<TABLE border=1 cellpadding=5 cellspacing=0 width=98%>\n");
print("<TR><TH colspan=3><A HREF=..>Up to Parent Folder</A></TH></TR>");
print("<TR><TH>File name</TH><th>File size</th><th>File date</th></TR>\n");

// loop through the array of files and print them all
for($index = 0; $index < $indexCount; $index++) {
    if(substr($dirArray[$index], 0, 1) !== "."){ // don't list hidden files (You could just use if($dirArray[$index] !== ".." && $dirArray[$index] !== ".") {
        print("<TR><TD><a href=\"".$dirArray[$index]."\">".$dirArray[$index]."</a></td>");
        print("<td>");
        print(size_hum_read(filesize($dirArray[$index])));
        print("</td>");
        print("<td>");
        print(date("F d Y H:i",filemtime($dirArray[$index])));
        print("</td>");
        print("</TR>\n");
    }
}

print("</TABLE>\n");

?>

 

1. Always use <?php and not the shorttag <? as some servers don't allow the shorttag.

2. Try and keep your code indented.

3. I removed an unused filesize command, and added the size_hum_read in the loop (output).

4. You don't need to put your parameters for print in brackets. Also, I would recommend echo instead (nothing is better, just personal preference).

Well I'll start by making your code readable by humans ::)

 

Haha, thanks.  In my heavy-handed and fat-thumb way, I was going fast and got lazy.  Thanks for cleaning up my mess.

 

1. Always use <?php and not the shorttag <? as some servers don't allow the shorttag.

Good tip, obviously my server does not mind, but good directions.

 

2. Try and keep your code indented.

See my first comment.  :)

 

3. I removed an unused filesize command, and added the size_hum_read in the loop (output).

Fat-thumbing a keyboard again.

 

4. You don't need to put your parameters for print in brackets. Also, I would recommend echo instead (nothing is better, just personal preference).

Thanks.

 

Thanks mattal999 for your help, it now functions as I wanted it to.  Your help is greatly appreciated.

Cheers

Bob a.k.a pineypl

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.