treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Display Styled Directory Contents

Last updated on:

Servers can be configured to show the contents of a directory that doesn't have an index file to render. The result is usually less than visually spectacular:

Lackluster default in Chrome

More better, View Demo

We can take control of this ourselves by replicating this functionality with PHP.

  1. Make an index file (.index.php, starting with the dot, really) which reads the files in the directory and outputs them into a table
  2. Make an .htaccess file that serves that file as the index
  3. Have the index file load in CSS and other resources that are also prefixed with a dot (hidden)

The following PHP reads the directory of files and displays a styled table of their name, file type, and file size. It also applies a class name in which to apply icons for the different major file types (see CSS).

<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Directory Contents</title>
  <link rel="stylesheet" href=".style.css">
  <script src=".sorttable.js"></script>
</head>

<body>

  <div id="container">
  
    <h1>Directory Contents</h1>
    
    <table class="sortable">
      <thead>
        <tr>
          <th>Filename</th>
          <th>Type</th>
          <th>Size <small>(bytes)</small></th>
          <th>Date Modified</th>
        </tr>
      </thead>
      <tbody>
      <?php
        // Opens directory
        $myDirectory=opendir(".");
        
        // Gets each entry
        while($entryName=readdir($myDirectory)) {
          $dirArray[]=$entryName;
        }
        
        // Finds extensions of files
        function findexts ($filename) {
          $filename=strtolower($filename);
          $exts=split("[/\\.]", $filename);
          $n=count($exts)-1;
          $exts=$exts[$n];
          return $exts;
        }
        
        // Closes directory
        closedir($myDirectory);
        
        // Counts elements in array
        $indexCount=count($dirArray);
        
        // Sorts files
        sort($dirArray);
        
        // Loops through the array of files
        for($index=0; $index < $indexCount; $index++) {
        
          // Allows ./?hidden to show hidden files
          if($_SERVER['QUERY_STRING']=="hidden")
          {$hide="";
          $ahref="./";
          $atext="Hide";}
          else
          {$hide=".";
          $ahref="./?hidden";
          $atext="Show";}
          if(substr("$dirArray[$index]", 0, 1) != $hide) {
          
          // Gets File Names
          $name=$dirArray[$index];
          $namehref=$dirArray[$index];
          
          // Gets Extensions 
          $extn=findexts($dirArray[$index]); 
          
          // Gets file size 
          $size=number_format(filesize($dirArray[$index]));
          
          // Gets Date Modified Data
          $modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
          $timekey=date("YmdHis", filemtime($dirArray[$index]));
          
          // Prettifies File Types, add more to suit your needs.
          switch ($extn){
            case "png": $extn="PNG Image"; break;
            case "jpg": $extn="JPEG Image"; break;
            case "svg": $extn="SVG Image"; break;
            case "gif": $extn="GIF Image"; break;
            case "ico": $extn="Windows Icon"; break;
            
            case "txt": $extn="Text File"; break;
            case "log": $extn="Log File"; break;
            case "htm": $extn="HTML File"; break;
            case "php": $extn="PHP Script"; break;
            case "js": $extn="Javascript"; break;
            case "css": $extn="Stylesheet"; break;
            case "pdf": $extn="PDF Document"; break;
            
            case "zip": $extn="ZIP Archive"; break;
            case "bak": $extn="Backup File"; break;
            
            default: $extn=strtoupper($extn)." File"; break;
          }
          
          // Separates directories
          if(is_dir($dirArray[$index])) {
            $extn="&lt;Directory&gt;"; 
            $size="&lt;Directory&gt;"; 
            $class="dir";
          } else {
            $class="file";
          }
          
          // Cleans up . and .. directories 
          if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;";}
          if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
          
          // Print 'em
          print("
          <tr class='$class'>
            <td><a href='./$namehref'>$name</a></td>
            <td><a href='./$namehref'>$extn</a></td>
            <td><a href='./$namehref'>$size</a></td>
            <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td>
          </tr>");
          }
        }
      ?>
      </tbody>
    </table>
  
    <h2><?php print("<a href='$ahref'>$atext hidden files</a>"); ?></h2>
    
  </div>
  
</body>

</html>

The resources loaded in that index file are the top-in table sorter script sortable.js and a .style.css file. (Remember, prefacing the files with a dot makes the invisible in most operating systems, and also won't show up in your directory of files (good)). Here's that CSS:

* {
	padding:0;
	margin:0;
}

body {
	color: #333;
	font: 14px Sans-Serif;
	padding: 50px;
	background: #eee;
}

h1 {
	text-align: center;
	padding: 20px 0 12px 0;
	margin: 0;
}
h2 {
	font-size: 16px;
	text-align: center;
	padding: 0 0 12px 0; 
}

#container {
	box-shadow: 0 5px 10px -5px rgba(0,0,0,0.5);
	position: relative;
	background: white; 
}

table {
	background-color: #F3F3F3;
	border-collapse: collapse;
	width: 100%;
	margin: 15px 0;
}

th {
	background-color: #FE4902;
	color: #FFF;
	cursor: pointer;
	padding: 5px 10px;
}

th small {
	font-size: 9px; 
}

td, th {
	text-align: left;
}

a {
	text-decoration: none;
}

td a {
	color: #663300;
	display: block;
	padding: 5px 10px;
}
th a {
	padding-left: 0
}

td:first-of-type a {
	background: url(./.images/file.png) no-repeat 10px 50%;
	padding-left: 35px;
}
th:first-of-type {
	padding-left: 35px;
}

td:not(:first-of-type) a {
	background-image: none !important;
} 

tr:nth-of-type(odd) {
	background-color: #E6E6E6;
}

tr:hover td {
	background-color:#CACACA;
}

tr:hover td a {
	color: #000;
}





/* icons for file types (icons by famfamfam) */

/* images */
table tr td:first-of-type a[href$=".jpg"], 
table tr td:first-of-type a[href$=".png"], 
table tr td:first-of-type a[href$=".gif"], 
table tr td:first-of-type a[href$=".svg"], 
table tr td:first-of-type a[href$=".jpeg"]
{background-image: url(./.images/image.png);}

/* zips */
table tr td:first-of-type a[href$=".zip"] 
{background-image: url(./.images/zip.png);}

/* css */
table tr td:first-of-type a[href$=".css"] 
{background-image: url(./.images/css.png);}

/* docs */
table tr td:first-of-type a[href$=".doc"],
table tr td:first-of-type a[href$=".docx"],
table tr td:first-of-type a[href$=".ppt"],
table tr td:first-of-type a[href$=".pptx"],
table tr td:first-of-type a[href$=".pps"],
table tr td:first-of-type a[href$=".ppsx"],
table tr td:first-of-type a[href$=".xls"],
table tr td:first-of-type a[href$=".xlsx"]
{background-image: url(./.images/office.png)}

/* videos */
table tr td:first-of-type a[href$=".avi"], 
table tr td:first-of-type a[href$=".wmv"], 
table tr td:first-of-type a[href$=".mp4"], 
table tr td:first-of-type a[href$=".mov"], 
table tr td:first-of-type a[href$=".m4a"]
{background-image: url(./.images/video.png);}

/* audio */
table tr td:first-of-type a[href$=".mp3"], 
table tr td:first-of-type a[href$=".ogg"], 
table tr td:first-of-type a[href$=".aac"], 
table tr td:first-of-type a[href$=".wma"] 
{background-image: url(./.images/audio.png);}

/* web pages */
table tr td:first-of-type a[href$=".html"],
table tr td:first-of-type a[href$=".htm"],
table tr td:first-of-type a[href$=".xml"]
{background-image: url(./.images/xml.png);}

table tr td:first-of-type a[href$=".php"] 
{background-image: url(./.images/php.png);}

table tr td:first-of-type a[href$=".js"] 
{background-image: url(./.images/script.png);}

/* directories */
table tr.dir td:first-of-type a
{background-image: url(./.images/folder.png);}

View Demo   Download Files

REMEMBER: The .zip file might appear to be empty, but it's not. The files are all prefaced with a dot. View them in a file editor which shows you "hidden" files.

Special thanks to Cliff White.

Update November 2012: The demo and downloadable files have been updated to 1) show more human readable file sizes 2) have error pages

View Comments

Comments

  1. Permalink to comment#

    You need to change line 136 to this to avoid a Parse error.

    a href='$dirArray[$index]'

    After that this works perfect! Thanks for sharing!

    //T

    • There were \” marks in there to make sure that wasn’t a problem but for whatever reason weren’t showing up in the displayed code. I just made them single ticks to avoid problems, thanks!

  2. Very nice script, thanks for sharing!

  3. nice script. I’m sure I can find something to use this for :)

  4. Looks really cool – I’ll definitely be using this for something or other.

  5. Permalink to comment#

    sweet. and so simple. thanks!

  6. fabuio
    Permalink to comment#

    Nice. Thanks.

    line 141:
    print(number_format((filesize($dirArray[$index])), 0, ”, ‘,’));

  7. Nice!

    One thing though: where can I find the little icons on the left of the file/folder please?

  8. Thanks for this. This is a great bit of code and very easy to add even for a php newb like me.

  9. I’m getting an error from this line: print(filesize($dirArray[$index]));
    I think it’s line 141 in the original, but I am just using the php part as an include with my own CSS so it’s line 48 in mine. Here is the error:

    Warning: filesize() [function.filesize]: stat failed for archives in /..(path to my website).org/includes/directory_contents.php on line 48

    I believe the error is because it is printing out the parent directory folder (and its parent, too), which have no filesize. It works great, except for those two lines – and I can’t figure out why they even are being printed, since they are not files IN the directory… My folder structure is /newsletters/archives/index.php and the first row of the table displaying the files shows “archives,” with the error under the filesize column, and the last row of the table shows “newsletters” and another filesize error.

    I’m just learning PHP, but I think a line to set the condition if there is no suffix, don’t include it might be fairly easy… for someone who is more fluent in PHP, anyway :)

    Anyone have any ideas? Thanks!

    • I’m trying to figure it out, but realized that setting a condition for not printing a file without a “.” in the string won’t work, because the “files” that are causing the errors don’t actually exist – it’s showing the parent and “grandparent” directories as if they were files IN the directory – /newsletters/archives/archives and /newsletters/archives/newsletters

      I can’t figure out what is causing that, and since they don’t actually exist in the directory, my idea for setting a condition of not printing a string without a “.” in it won’t have any effect. And it hasn’t.

      Here’s the page:
      http://bristleconecnps.org/newsletters/archives/index.php

  10. I got it to stop displaying the empty line at the top and the first error – the archives one, by changing the loop to this:

    for($index=4; $index < $indexCount; $index++) {

    starting at “1″ instead of 0 got rid of the blank, and starting at “4″ takes into consideration the two parent directories which aren’t being shown plus the weird error one, and starts on the first real file. There is still the error at the end (last row) of the table.

    Okay, my head hurts, and I hope some of you wonderful PHP people and problem solvers have some better ideas!

    Thanks.

    • I just fixed it:
      for($index=4; $index < $indexCount-1; $index++) {
      (added -1 to the index count so it wouldn’t show the last “file”)

      This is obviously a workaround that is specific to that file structure – if the directory was at a different level it would need to start at a different place, which makes using this code as an include for any directory index file I want on my site not as simple, but hey, it works!

      Sorry for all the multiple comment posts, but I hope it may be helpful and/or interesting to someone! :)

      And if someone has a better idea that will fix it instead of hiding it, I would love to hear it.

      Thanks!!

    • This will prevent the last file from being listed in the directory. Do you have any idea on how to prevent the index.php from showing?

    • cnwtx
      Permalink to comment#

      Put this line in your .htaccess file:

      DirectoryIndex  index.html index.htm default.htm index.php .index.php 

      Then rename the file to .index.php (with the leading dot). The php script script will then prevent the showing of hidden files. The result: your index.php will not show up in the listings.

  11. Very cool. Thank you very much Chris. I know I’m late in the game here but I’m definitely gonna use your script.

  12. OK… I’ve played with this and it works great. Really excited about this but I don’t know enough to start adding stuff to the code. But what about adding a download function to the directory? Each of the items would get their own download button. Just an idea in case someone would like to try it. Thanks again for the script!

  13. Chris
    Permalink to comment#

    Hey this is great code works so sweet!

    But I need to open up a different dir to where the script is located how do i do that?

  14. Tom S
    Permalink to comment#

    Cannot get this to work. It only displays the headers on my page… no file listings, and there are plenty of files in the directory.

    Any thoughts?

    • Zeyad
      Permalink to comment#

      Dear all :
      You can choose a certain file only , for example if you want to display “.doc” only without any files maybe exist in that folder, so then you should REPLACE this code instead of gets each entry to be seems as follow:

      // gets each entry
      while($entryName = readdir($myDirectory)) {
      if (strpos($entryName, ‘.doc’,1) ) {
      $dirArray[] = $entryName;
      }
      }

      Zeyad

  15. David
    Permalink to comment#

    Anyone know how to get it to NOT display certain given files such as the favicon or the index.php file?

    Better still for me: how do i make display ONLY the folder names?

    Useful script. Thanks.

    David

    • Permalink to comment#

      Hi David,

      I had a closer look at this script, very useful indeed… my suggestion for you are:

      Define (just before the while-loop): $forbiddenExts = array(“php”, “ico”, “html”);
      In the while-loop, do the same as in findexts():

      while($entryName = readdir($myDirectory)) { 
      $exts = explode(".", $entryName);
        if(!in_array($exts[1],$forbiddenExts)) { 
          $dirArray[] = $entryName;
        }
      }

      And if you want to display only folders, then add another line of code just before the first if:

      if (!is_file($entryName)) {
      // the lines above
      }

      Altogether:

      
      // opens this directory
      			$myDirectory = opendir(".");
      			
      			// set forbidden files
      			$forbiddenExts = array("php", "ico", "html");
      			
      			// gets each entry
      			while($entryName = readdir($myDirectory)) {
      				if (is_file($entryName)) {
      					$exts = explode(".", $entryName);
      					if(!in_array($exts[1],$forbiddenExts)) { 
      						$dirArray[] = $entryName;
      					}
      				}
      			}
      
    • Permalink to comment#

      !is_file()… you don’t want to display files… in my script I don’t want to display folders… ;-)

  16. heath
    Permalink to comment#

    great script, but i can’t figure out how i can change the folder.
    every time i change the directory there will appear a mistake in that in in which sort($dirArray); located.

    what i’m supposed to do?

  17. just another method i have used before, which displays the contents of a folder, and gives a link and preview… if anybody wants the code, let me know. example is http://www.inktecbots.com/specials.php

    • Terry
      Permalink to comment#

      Hello, I like the look of your example, can you please let me have the code

  18. jassi
    Permalink to comment#

    hi..
    i m just learning to use php… this code is gr8…
    i m getting the parse error… so what is the change in line 136?
    thanx

  19. jon
    Permalink to comment#

    hey, im getting this to work really well, but what if i wanted to display folders and files, but excluding the php and icon files? what would i need to change?

  20. jon
    Permalink to comment#

    right…but in some instances i want it to display both files AND folder, but NOT the *.ico and *.php files

  21. Life. Saver.
    I have customized the script to create a simple with links from given folders. You don’t know how this helped me out, thanks a lot.

  22. Prad

    Thanks buddy! helped me a lot

  23. Martin
    Permalink to comment#

    if one chances the line $myDirectory line to:

    $myDirectory = opendir(“c:\docs”);

    The files in the docs directory are listed correctly, however, when one clicks on the link the link ties to load the file from the localhost directory.

    Is there an easy way of linking to the the files in the c:\docs directory?

    Thanks

  24. Gokul
    Permalink to comment#

    Thanks for this piece of code. Cute one

  25. Akhilaa
    Permalink to comment#

    Im learner for php. This code show some error. the extenson of this file is .php rite?

  26. Akhilaa
    Permalink to comment#

    excatly wht my requirement is to dispaly the file in particular local folder in webpage…. so tht clicking on the links the file shuld get downloaded….. pls can anyone help me in doin this?. i need in html

  27. Permalink to comment#

    This is not working for me. I used it as it is in php 5 but i is not working at all. what should I modify?

  28. Permalink to comment#

    I’m having an issue displaying the file size when I change the directory.

    http://infasyssolutions.com/control.php

    Above is what I’m trying to make work. If i leave it to the (“.”) everything works perfect, but when I change it to (“images”) everything but the file size works. Any ideas as to why?

  29. Ann Rose
    Permalink to comment#

    Hai.. this code helps me alot..
    but i need to listout folders in the directory and also if there is any documents in that folder that also should be listout..please help me

  30. Jonathan
    Permalink to comment#

    I found a fix the issue with changing the directory.
    The problem was that changing the directory prevented the hyperlink to the file from working, along with preventing the PHP processor from accessing the file to read its size. Even though it still listed the files in the folder as intended.
    Do the following:
    ————————————————————————–
    *Replace the lines:

    
    // opens this directory
    $myDirectory = opendir(".");
    

    with the lines:

    
    $directory = "./";  // or example: $directory = "uploads/";
    // opens this directory
    $myDirectory = opendir($directory);
    

    ————————————————————————–
    Also replace:

    
    $exts = split("[/\\.]", $filename) ;

    with

    
    $exts = split("[/\\.]", $filename) ;
    //$exts = explode("[/\\.]", $filename) ;

    You may need to use if you version of PHP is 5.3 or later.
    ————————————————————————–
    Replace:

    
    <a href='$dirArray[$index]'

    with:

    
    <a href='$directory$dirArray[$index]'

    ————————————————————————–
    and replace:

    
    filesize($dirArray[$index])

    with

    
    filesize($directory.$dirArray[$index])
  31. We would like to thank you for the great script you have made. Specially the thing with transperent effeect. It’s one of the coolest thing on earth.

  32. Rolland
    Permalink to comment#

    Is there any way to load a directory from an external domain?

  33. Jeff
    Permalink to comment#

    Great piece of work which no doubt will help many people.
    I also found the reworking of the script to show the contents of a different folder/location to be very helpful as it overcomes the problems of showing certain file types which you may not otherwise wish to make viewable.
    I did however find one small issue after the rework in as much as the icons for the file types weren’t displaying in the directory listing … and in my case, having put the icons in a folder called ‘img’ I simply had to change the following lines 60 through to 89 …

    I’ll only give one example to save space :

    71 /*docs*/
    72 a[href$=".doc"] {background: url(img/doc.gif) no-repeat left 50%;}
    73 a[href$=".txt"] {background: url(img/doc.gif) no-repeat left 50%;}
    74 a[href$=".rtf"] {background: url(img/doc.gif) no-repeat left 50%;}

    I hope this helps … even just one person …

    Anyway … MAGNIFICENT script … MANY THANKS ;-)

  34. atarubi
    Permalink to comment#

    To get filesize of a file from another directory :

    $dirname = "./YourFolderName" //Change "YourFolderName" to your folder's name
    $filez = $dirArray[$index]; //Get the name of file
    $path = $dir.'/'.$filen; //Path address of the file (included the directory path)
    $size = filesize($path); //Get the size of a file
    print("$size"); //print the size

    I use PHP 5.2.10, it works fine .

  35. HI, very nice script – thanks a lot.
    I have created a page that shows the latest pdf on my web using google docs viewer.
    Could the code be added to the script, so that a “view online” link is added to the file list?

    The code I´m using is as follows:
    ” iframe src=”http://docs.google.com/gview?url=http://www.benitezjones.com/uploads/ultima.pdf&embedded=true” style=”width:718px; height:700px;” frameborder=”0″

    Any ideas?

  36. Permalink to comment#

    How to display the last modified date?

    • cnwtx
      Permalink to comment#

      Add this line inside the while loop:

      $modtime=date("M j Y g:i A", filemtime($dirArray[$index]));

      And then this wherever you want to output that time (still must be inside the loop):

      print("$modtime");
  37. Permalink to comment#

    How do you also create a css archive by adding thumbnail using first image and excerpt of a file.

  38. Zeyad
    Permalink to comment#

    Dear All;
    thanks Jesus
    if body can help that question
    How to display the last modified date
    ————————————————–

    Question to Help :

    How to display results in limit for 30 results per page – infact this to large reults inside same folder
    any help
    please

  39. Joel
    Permalink to comment#

    Thanks a lot. Looking everywhere for one of these that worked, this code is perfect. Appreciate your addition to hide specific file types as well. Exactly what I needed.

  40. Zeyad
    Permalink to comment#

    How to display results in limit for 30 results per page – infact this to large reults inside same folder
    any help
    please

  41. Permalink to comment#

    Is there a way to mark the directories in that directory as such.I use words ,letters and it would be helpful if they were marked somehow.This is the best Script of this type I have found.I have looked high and low for this and came across this purely by accident.Thanks Andrew for posting this for others to use

  42. Zeyad
    Permalink to comment#

    Dear all :
    You can choose a certain file only , for example if you want to display “.doc” only without any files maybe exist in that folder, so then you should REPLACE this code instead of gets each entry to be seems as follow:

    // gets each entry
    while($entryName = readdir($myDirectory)) {
    if (strpos($entryName, ‘.mp3’,1) ) {
    $dirArray[] = $entryName;
    }
    }

    So, the files of mp3 will be displayed only

    and if you want to display files of doc , or html , or wav, or ram , you should replac mp3 with any of them

    Zeyad

    • Chad
      Permalink to comment#

      Zeyad, do you know how to reverse this process so I can Hide mp3 files?

  43. Zeyad
    Permalink to comment#

    But really i want some one to tell me how to limit this display , if the folder havre large quantities of mp3 files
    and want to display 20 files per page when display in browsers

    thanks in advance

  44. Permalink to comment#

    Just came across this. Brilliant. Just what I wanted and as a beginner in php, a nice intro to some basic functionality.
    Thanks.

  45. Permalink to comment#

    File size change, bytes to Kbytes!
    Replace:

    print(filesize($dirArray[$index]));

    with:

    print(round(filesize($dirArray[$index]) / 1024, 2));

    ;)

  46. Commenter
    Permalink to comment#

    Hello, I get this without modifying the code at all:
    Deprecated: Function split() is deprecated in C:\xampp\htdocs\test\.index.php on line 39

    I get a new one of those every time I add a new folder, and when I click on hidden folders I get the same line just a bunch of more times.

    • Bru
      Permalink to comment#

      Read the comments above. ..split() is replaced with explode() in the latest PHP builds.

  47. Bru
    Permalink to comment#

    I guess no one is using the “file type” column which didn’t work for me. It seemed to be printing out the full filename rather then the extension so I added the substr function to count -3 back from the end of the filename and then display 3 characters to the right. New code looks like this.

    
             // finds extention of file
             function findexts($filename)
             {
               $filename = strtolower($filename) ;
               $exts = explode("[/\\.]", $filename) ;
               $n = count($exts)-1;
               $exts = $exts[$n];
    	   $exts = substr($exts, -3, 3);	 //added in				 
    	    return $exts;
             }
    

    Hope this helps anyone needing this.

  48. Qais Patankar
    Permalink to comment#

    Change $exts=split("[/\\.]", $filename);on line 39 to $exts=explode("[/\\.]", $filename);

  49. Mihai
    Permalink to comment#

    I just uploaded it here
    http://whatshappeningtampa.com/events/

    i created a test directory but when i try to acess that it shows an error
    anything missing from the code ? or i cant have subfolders

    • Permalink to comment#

      I think i’m having the problem you had but from your example it look like you’ve solved it. I can style the first directory, but once you move to another folder, the styles dont stay, but rather they default back to the ugly standard layout. Can you share how to get my subfolders to share the same styling?

  50. Commenter
    Permalink to comment#

    Hey, I get this when I set:

    $myDirectory=opendir("./folder");

    Every time I add a new file, I get a new set of those 3 errors.

    
    Warning: filesize() [function.filesize]: stat failed for Ny mappe in C:\xampp\htdocs\ny\.index.php on line 76
    
    Warning: filemtime() [function.filemtime]: stat failed for Ny mappe in C:\xampp\htdocs\ny\.index.php on line 79
    
    Warning: filemtime() [function.filemtime]: stat failed for Ny mappe in C:\xampp\htdocs\ny\.index.php on line 80
    
    Warning: filesize() [function.filesize]: stat failed for Ny mappe (2) in C:\xampp\htdocs\ny\.index.php on line 76
    
    Warning: filemtime() [function.filemtime]: stat failed for Ny mappe (2) in C:\xampp\htdocs\ny\.index.php on line 79
    
    Warning: filemtime() [function.filemtime]: stat failed for Ny mappe (2) in C:\xampp\htdocs\ny\.index.php on line 80
    
  51. Mo
    Permalink to comment#

    Hi!

    this is great. Thank you.

    Is there a way to have the list sorted alphabetically by default? Not it seems to be sorted by modified date. Is this easy to adjust?

    Many thanks!

  52. Is there anyway to keep the styling when you enter a folder on the server?

  53. Permalink to comment#

    Hi,
    I am using this brilliant code for directory listing with thumbnails using imagemagick for PDF thumbnails.

    the issue is that each time I use the following loop a new jpg file is created. even if it already there..

    Please urgent help required…..

    $path =”images/”;
    $filename = “$path/$namehref.jpg”;

    if (file_exists($filename))
    {
    $tempimg = $filename;
    }
    else
    {
    $result = exec(“convert -density 300 $path/$namehref -thumbnail 150×150 $path/$namehref.jpg”);
    $tempimg = “$path/$namehref.jpg”;
    //$tempimg = “images/preview.jpg”;
    }

    //****using for thumbnail display***/

  54. jenderalcilik
    Permalink to comment#

    hi, im a total noob about coding. so, excuse my stupid question. where should i put those files? i tried to upload the on the root but nothing is happening.

  55. yh

    Thanks for the script, it works well. Is there a way to show the same style after clicking on the folders in the same directory as files?

    • yh
      Permalink to comment#

      to keep the same style when open subdirectories, i managed it work for me:

      in the beginning of this page set a variable to receive a directory

      in the FOR LOOP, i checked the filetype,

      if filetype($namehref) == ‘file’
      print
      if filetype($namehref) == ‘dir’
      link back the same page by using <a href=$PHP_SELF?dir =directory

      you might want to play with the directory to meet your needs.

    • Permalink to comment#

      I’m having issues getting the sub folders to share the same styling. My root folder looks great, but as soon as I click to another folder I get the defaults back again. Could you explain more about how to build the for loop and share an example. Sorry I’m not the best in php.

  56. Chuck
    Permalink to comment#

    yh would you care to post a copy of your script that you used to maintain the formatting when entering a directory. I’ve tried the example that you posted but I keep getting a parse error.

    Cheers

  57. Tim
    Permalink to comment#

    A great script.. thank you….it worked first go for me however I have run into an issue that I think relates to the script focusing on file extensions of 3 characters only.
    I ran into an issue where I have .docx and .xlsx files located on the server. While it presents them correctly in the list on the page, when I click to download them the Save window defaults to save them as .zip instead of the original 4 letter extension.

    Are there any changes to the script you can recommend to resolve this?

    thank you in advance

    Tim

    • Tim
      Permalink to comment#

      oowww just realised I get these results only within IE8 but Chrome seems to work fine.

  58. Tung

    Hi all,
    I just deployed this script.

    Demo is here http://hopquacuoi.com/kt/

    Anyone can help me to embed this script with
    1. Delete function (can delete/remove displayed file)
    2. Preview function (can preview a file)
    3. Alert function. I want to embed a reminder like this http://www.fremeaujewelers.com/reminder/ into.

    Many thanks
    Tung

  59. Permalink to comment#

    I’m trying to also password protect this site at manabreakfast.com/commissions and when I add the code to the .htaccess file and upload the .htpasswd file it gives me a 500 error. Is there something else i need to do to password protect this directory and use this styled directory contents?

  60. George
    Permalink to comment#

    Hello there, this is great!

    One issue though….My browser wont show the file or force download. I get a 403 error. Please help! This is amazingg….

  61. Ben
    Permalink to comment#

    Hy guys,

    Thank you a lot for this work
    I ‘am getting same warning and try a lot of things to fix it
    No solutions
    Please help me if you can
    this is warning :
    Warning:
    filesize() [function.filesize]: stat failed for aigle_005.jpg in C:\wamp\www\Tests\.index.php on line 79

    Warning: filemtime() [function.filemtime]: stat failed for aigle_005.jpg in C:\wamp\www\Tests\.index.php on line 82

    the both functions are not working

    Thank you

  62. Permalink to comment#

    Hi I was wondering if there is any modification to this script that will force the user to download each file (PDF, JPG etc.) rather than open it in the browser window?

    Thanks, and great script by the way!

  63. Hellbeast58
    Permalink to comment#

    I just downloaded and extracted all the files individually into my folders that i want to use this in, and i dont see a change, i only see the default index page, is there something i am missing? or did i not change something within the files to make it work that you didnt explain well enough for me, please help, thanks if you reply/e-mail me as well.

    by the way, i only know basic HTML coding, but i run my own webserver from my home.

    • Jared Honsvick
      Permalink to comment#

      I believe the files you need are hidden files ( files starting with a “.” period. ). Configure your computer to show hidden files and you’ll see the hidden files in the download folder.

  64. Hellbeast58
    Permalink to comment#

    first, thanks for the reply!
    second, thats not my issue, i use winrar to open the zip, and can see, move, and modify the files, they just wont load in my website, and if it matter im running it with apache 2.2 on ubuntu 12.04 LTS

    • bud
      Permalink to comment#

      Do you have php enabled and using the correct chmod settings?

  65. Hellbeast58
    Permalink to comment#

    doubt it if thats something extra you have to change, any way to help me with that to see if thats the issue?

    • Jared Honsvick
      Permalink to comment#

      Hmmm….Not sure about that one. I loaded the files onto my server and it just worked. Sorry….Good luck.

  66. Hellbeast58
    Permalink to comment#

    ok so i got php5 installed now, is there anything else i could try “bud”?

  67. bud
    Permalink to comment#

    Personally,I have no idea how to set php up on a server.But if you have that done test it to make sure it works.Try a couple of easy php scripts and then move on to this one.

  68. Hellbeast58
    Permalink to comment#

    just so you know if using linux to host the server

    sudo apt-get install apache2
    sudo apt-get install php5
    sudo apt-get install libapache2-mod-php5
    sudo /etc/init.d/apache2 restart

    and i also got it working by point the links on the page to the actual .index.php file, but for some reason it would not point to the file automatically, had to make the link like normal

  69. Permalink to comment#

    Using html how to open display directory contents in tomcat server..pls help me

  70. I am trying to find out the exact code to replace so I can search for all files with names starting E05 H06.mp4 I would like to Deliver files only that are E05. Any help would be appreciated

  71. Lance
    Permalink to comment#

    Having the same issue as Jared. Would like to keep the styling within the folders that are listed. Anyway to do that? Yh presented somewhat of a solution but i have no way of implementing that as I am a PHP novice.

    • Lance
      Permalink to comment#

      Hmm….it could be as easy as putting all of the original folders and files to make this happen in each subdirectory?

    • Lance
      Permalink to comment#

      Hmm….it could be as easy as putting all of the original folders and files to make this happen in each subdirectory?

      …..nope that is way too time consuming and takes a lot when you have a pretty elaborate directory tree structure.

    • Lance
      Permalink to comment#

      Alright. I did it the hard way. I copied the .index.php file into the sub folders and changed out the link to the sortable.js and the .css file and it worked like a charm. I’m much more fluent with css and html than I am in php. I’m sure there is a better way to do this….but until then I have to use this.

  72. josue contreras
    Permalink to comment#

    the script is working in my website, but the icons are not working. can some one be so kind as to give the steps to install this script. I am a bit slow on php. the specific step by step instruction will realy help.

  73. bud
    Permalink to comment#

    You have to make sure the path to the icon images is correct.I use this script in multiple locations but the images for the icons I use the full location address-no shortcuts so that I can use the script this way

  74. josue contreras
    Permalink to comment#

    Where do you find the path for the icon images. What i did is this:
    1. I downloaded the zip file
    2. I extract the content
    3. I took the content of the file and put all the content in the file on the server that I want to generate links.
    4.I directed the page to load on:
    http://www.exeple.com/none/.index.php

    It works but the images on the left does not appear.
    I know I am doing something wrong. This is a freat code. If i dont figure it out, it will be ok, i will use it as it is now. but i will never learn if I dont ask, and I just dont want to half way do something. Your help is greatly appricated. thanks

  75. bud
    Permalink to comment#

    Don’t forget that the directories have a dot in front of them – .images

    If you got rid of that dot in front of the folder them it wont work.I see there are limited icons that come with this script.The css file is where the images are set for the file types.make sure it points to the correct location as I mentioned above

  76. bud
    Permalink to comment#

    By the way,the page you posted does not show the script you are talking about.There is nothing on that page that I saw was relevent to the script either????????

  77. josue contreras
    Permalink to comment#

    sorry, but the website was just an exemple is not an active website, sorry for that. I see what you mean about the css file to point to the .images folder. I have not done that. thanks a lot will try it and let you know. thanks again. very helpful.

  78. Tide
    Permalink to comment#

    Hello
    I wonder if this code allows you to hide files with. Html or. Php in the same directory as the images

    I thank you in advance for help

    • Chad
      Permalink to comment#

      Tide, I agree. I see people adding code to only show mp3 files or doc files. I would love to reverse that so we DON’T show php files.

  79. Permalink to comment#

    Thanks mate! I have removed the “Show Hidden Files” function for security reasons.

  80. DebbyP
    Permalink to comment#

    Ralph, how did you modify to remove the “Show Hidden Files”?

    • DebbyP
      Permalink to comment#

      I tried the solution above (Benjamin September 29, 2010) and got all the individual files, no folders (not what I want) and when I clicked on the show hidden files they were all there so that didn’t work for me.

  81. DebbyP
    Permalink to comment#

    Anyway to make it work in all directories and sub directories without having to copy the code into each directory?

  82. DebbyP
    Permalink to comment#

    Looking to make it work somewhat like this ftp://triad-atlanta.com/

Leave a Comment

Use markdown or basic HTML and be nice.