Page 1 of 1

File listings with images and file sizes

Posted: Tue May 07, 2002 5:53 pm
by gotDNS
Hey, I'm trying to make a page that lists the files in a certain folder, but also shows an image for each file type, tells the file type, and shows the file size. I have the list part down at http://brin.ath.cx/ but I cant get it to show the file size or anything else. Please help. The PHP file that lists the files is at http://brin.ath.cx/ . Thanks

Posted: Wed May 08, 2002 8:11 am
by enygma
cant you just use filesize() to get the size and output it?

http://www.php.net/manual/en/function.filesize.php

-enygma

Posted: Wed May 08, 2002 8:53 am
by mikeq
This is a piece of code I use, it is modified from PHPNuke.

Code: Select all

function displaydir($Co=null,$UserDir=null,$Company=null)
     {
     Global $cookie;
     $basedir = $Co;
     $wdir = $UserDir;
     global $udir;
     global $lastaction;
     $lastaction = "Listing directory";
     $iconget = "<IMG src="images/disk.jpg" alt="Click to download" border="0">";
     echo "<TABLE BORDER="0" cellspacing="1" cellpadding="0" width="500">";
     print "<tr><td colspan="5" class="rightnav-links">To download a file click on <IMG src="images/disk.jpg" alt="Click to download" border="0" height="20" width="20"> next to your file</td></tr>";
     print "<tr><td colspan="5" class="rightnav-links">To delete a file click on <img src="images/delete.jpg" alt="Delete $file" border="0" height="20" width="20"> next to your file</td></tr>";
     print "<tr><td colspan="5">&nbsp</td></tr>";     
     echo "<tr>";
     echo "<td class="directory-intsite">Type</th>";
     echo "<td class="directory-intsite">Name</th>";
     echo "<td class="directory-intsite">Size</th>";
     echo "<td class="directory-intsite">Modified</th>";
     echo "<td class="directory-intsite">Action</th>";
     echo "</tr>";
     //print "Base: $basedir User: $wdir";
     //print $basedir . $wdir;
     chdir($basedir . $wdir);
     $handle=opendir(".");
     while ($file = readdir($handle))
          &#123;
          if(is_dir($file)) $dirlist&#1111;] = $file;
          if(is_file($file)) $filelist&#1111;] = $file;
          &#125;
     closedir($handle);
     if($filelist)
          &#123;
          asort($filelist);
          while (list ($key, $file) = each ($filelist))
               &#123;
               if (ereg(".gif|.jpg",$file))
                    &#123;
                    $icon = "<IMG src="images/image.gif" alt="Image" border="0">";
                    $browse = "1";
                    $raw = "0";
                    $image = "1";
                    &#125;
               elseif (ereg(".txt|.dat",$file))
                    &#123;
                    $icon = "<IMG src="images/paper.jpg" alt="Text" border="0">";
                    $browse = "1";
                    $raw = "1";
                    $image = "0";
                    &#125;
               elseif (ereg(".wav|.mp2|.mp3|.mp4|.vqf|.midi",$file))
                    &#123;
                    $icon = "<IMG src="images/audio.gif" alt="Audio" border="0">";
                    $browse = "1";
                    $raw = "0";
                    $image = "0";
                    &#125;
               elseif (ereg(".phps|.php|.php2|.php3|.php4|.asp|.asa|.cgi|.pl|.shtml",$file))
                    &#123;
                    $icon = "<IMG src="images/webscript.gif" alt="Web program" border="0">";
                    $browse = "1";
                    $raw = "1";
                    $image = "0";
                    &#125;
               elseif (ereg(".htaccess",$file))
                    &#123;
                    $icon = "<IMG src="images/security.gif" alt="Apache Webserver security settings" border="0">" ;
                    $browse = "0";
                    $raw = "1";
                    $image = "0";
                    &#125;
               elseif (ereg(".html|.htm",$file))
                    &#123;
                    $icon = "<IMG src="images/webpage.gif" alt="Web page" border="0">";
                    $browse = "1";
                    $raw = "1";
                    $image = "0";
                    &#125;
               else
                    &#123;
                    $icon = "<IMG src="images/unknown.jpg" alt="Unknown filetype" border="0">";
                    $browse = "1";
                    $raw = "1";
                    $image = "0";
                    &#125;
               $filename=$basedir.$wdir."&quote;.$file;
               $fileurl=rawurlencode($wdir.$file);
               $fileurl2=rawurlencode($udir.$wdir.$file);
               $lastchanged = filemtime($filename);
               $changeddate = date("d-m-Y H:i:s", $lastchanged);
               echo "<TR>";
               echo "<TD align="center" nobreak>";
               echo "$icon</TD>
";
               echo "<TD nobreak class="filename">". htmlspecialchars($file)."</TD>
";
               echo "<TD align="right" nobreak class="filedetail">" . display_size($filename) . "</TD>";
               echo "<TD align="middle" nobreak  class="filedetail">" .$changeddate;
               echo "</TD><TD nobreak>";
               if($browse == "1")
                    &#123;
                    echo " <A HREF="../../datadownload/$Company$wdir/$file">$iconget</A> ";
                    echo "<A HREF="download.php?op=del&wdir=$Company$wdir&file=$file"><img src="images/delete.jpg" alt="Delete $file" border="0"></A> ";
                    &#125;
               &#125;
          &#125;
     echo "</TD></TR></TABLE>";
&#125;

function display_size($file)&#123;
    $file_size = filesize($file);
    if($file_size >= 1073741824)
      &#123;
        $file_size = round($file_size / 1073741824 * 100) / 100 . "g";
     &#125;
    elseif($file_size >= 1048576)
     &#123;
        $file_size = round($file_size / 1048576 * 100) / 100 . "m";
     &#125;
    elseif($file_size >= 1024)
     &#123;
        $file_size = round($file_size / 1024 * 100) / 100 . "k";
     &#125;
    else&#123;
        $file_size = $file_size . "b";
     &#125;
    return $file_size;
&#125;

Posted: Wed May 08, 2002 9:23 am
by enygma
This is a piece of code I use, it is modified from PHPNuke.
....which explains why it's friggin' huge! </head! pants! now!>

Whats all that?

Posted: Wed May 08, 2002 2:07 pm
by gotDNS
I guess im only good with if statements...lol.....btw, i already looked at the manual for filesize() but thats why i asked for help, i wish they gave examples. I'll try to underastand all of that code, but in case....can anyone give me a 'snipit' of code on how to get the file size of a file called....oh say....bob.html

Thanks guys!

Posted: Wed May 08, 2002 2:23 pm
by enygma
hmmm...how hard is "filesize ( string filename)" ?

$size=filesize("bob.html");
echo $size;

pretty tough there ;)
-enygma

Posted: Thu May 09, 2002 3:08 am
by mikeq
enygma wrote: ....which explains why it's friggin' huge! </head! pants! now!>
:mrgreen:

Odd...

Posted: Thu May 09, 2002 7:58 pm
by gotDNS
That's odd, i had tried that before enygma, but i guess i had a syntax error somewhere. I have a 69% in Afro-Asain class, and a 79 in Geometry, lighten up. lol.