How do i list contents of a folder to download

  • Irshad
  • Born
  • Born
  • Irshad
  • Posts: 1
  • Loc: Colombo Sri Lanka

Post 3+ Months Ago

Hi,

I have a folder called docs uploaded in my site. what i need is a way to list the contents of this folder for the visiters to download them. As i am planing to have lots of docs in this folder is there a simple way to list the contents of this folder rather than me creating links to each individual document i upload everytime.

Thank you...!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

what is your server on, windows or *nix??

you can use php if you want to customize how it looks. So that way it
would match the look and feel of your website design. However, if you
really don't care about how it looks, then htaccess is the easiest way to
get around that.

Anyone have that code handy? I can't remember it...[sorry]
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

normally if you just leave an index.* out of the directory and link to it, a directory tree listing will show up.

but I think the glob() function in php can also achieve a look if you want to hardcode it into a page.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9088
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

If you are on a Unix/Linux server and you use Apache and can use .htaccess files the following might help. This post explains how make so your directories show the contents of the files in it, that is if your server is preventing you from showing the contents of a directory right now.

http://www.ozzu.com/viewtopic.php?t=517
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

thx bigweb, i could have searched myself i guess :oops:
  • Photonomad
  • Born
  • Born
  • User avatar
  • Posts: 2

Post 3+ Months Ago

I know this topic is a little old, but I found a great PHP solution somewhere else on the web (unfortunately I can't give them credit because I can't remember where I found it).

Put this code in a file named index.php inside the directory where you want to list the contents. Make sure the path to the directory is correct.... it is probably case sensitive and you should check with your host if you have any problems... I was hung up at this stage for a while because I was writing the path incorrectly.
PHP Code: [ Select ]
 
<?
 
     
 
   /**
 
    * Change the path to your folder.
 
    *
 
    * This must be the full path from the root of your
 
    * web space. If you're not sure what it is, ask your host.
 
    *
 
    * Name this file index.php and place in the directory.
 
    */
 
                     
 
    // Define the full path to your folder from root
 
   $path = "put the full path here";
 
                     
 
    // Open the folder
 
   $dir_handle = @opendir($path) or die("Unable to open $path");
 
                     
 
   // Loop through the files
 
   while ($file = readdir($dir_handle)) {
 
                     
 
   if($file == "." || $file == ".." || $file == "index.php" )
 
                     
 
   continue;
 
   echo "<a href=\"$file\">$file</a><br>";
 
                     
 
   }
 
                     
 
   // Close
 
   closedir($dir_handle);
 
                     
 
?>
  1.  
  2. <?
  3.  
  4.      
  5.  
  6.    /**
  7.  
  8.     * Change the path to your folder.
  9.  
  10.     *
  11.  
  12.     * This must be the full path from the root of your
  13.  
  14.     * web space. If you're not sure what it is, ask your host.
  15.  
  16.     *
  17.  
  18.     * Name this file index.php and place in the directory.
  19.  
  20.     */
  21.  
  22.                      
  23.  
  24.     // Define the full path to your folder from root
  25.  
  26.    $path = "put the full path here";
  27.  
  28.                      
  29.  
  30.     // Open the folder
  31.  
  32.    $dir_handle = @opendir($path) or die("Unable to open $path");
  33.  
  34.                      
  35.  
  36.    // Loop through the files
  37.  
  38.    while ($file = readdir($dir_handle)) {
  39.  
  40.                      
  41.  
  42.    if($file == "." || $file == ".." || $file == "index.php" )
  43.  
  44.                      
  45.  
  46.    continue;
  47.  
  48.    echo "<a href=\"$file\">$file</a><br>";
  49.  
  50.                      
  51.  
  52.    }
  53.  
  54.                      
  55.  
  56.    // Close
  57.  
  58.    closedir($dir_handle);
  59.  
  60.                      
  61.  
  62. ?>
  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Thank you *Photonomad, I was just looking for something like this. Your code will do fine for me. There was a very small typo in the code, that was causing to not show any link to the files. Here is the fixed code.

PHP Code: [ Select ]
<?
 
 
 
/**
 
* Change the path to your folder.
 
*
 
* This must be the full path from the root of your
 
* web space. If you're not sure what it is, ask your host.
 
*
 
* Name this file index.php and place in the directory.
 
*/
 
 
 
// Define the full path to your folder from root
 
$path = "full path to folder";
 
 
 
// Open the folder
 
$dir_handle = @opendir($path) or die("Unable to open $path");
 
 
 
// Loop through the files
 
while ($file = readdir($dir_handle)) {
 
 
 
if($file == "." || $file == ".." || $file == "index.php" )
 
 
 
continue;
 
echo "<a href=\"$file\">$file</a><br>&gt;";
 
 
 
}
 
 
 
// Close
 
closedir($dir_handle);
 
 
 
?>
  1. <?
  2.  
  3.  
  4.  
  5. /**
  6.  
  7. * Change the path to your folder.
  8.  
  9. *
  10.  
  11. * This must be the full path from the root of your
  12.  
  13. * web space. If you're not sure what it is, ask your host.
  14.  
  15. *
  16.  
  17. * Name this file index.php and place in the directory.
  18.  
  19. */
  20.  
  21.  
  22.  
  23. // Define the full path to your folder from root
  24.  
  25. $path = "full path to folder";
  26.  
  27.  
  28.  
  29. // Open the folder
  30.  
  31. $dir_handle = @opendir($path) or die("Unable to open $path");
  32.  
  33.  
  34.  
  35. // Loop through the files
  36.  
  37. while ($file = readdir($dir_handle)) {
  38.  
  39.  
  40.  
  41. if($file == "." || $file == ".." || $file == "index.php" )
  42.  
  43.  
  44.  
  45. continue;
  46.  
  47. echo "<a href=\"$file\">$file</a><br>&gt;";
  48.  
  49.  
  50.  
  51. }
  52.  
  53.  
  54.  
  55. // Close
  56.  
  57. closedir($dir_handle);
  58.  
  59.  
  60.  
  61. ?>
  • Photonomad
  • Born
  • Born
  • User avatar
  • Posts: 2

Post 3+ Months Ago

Thanks quantumcloud for catching the mistake in my code!

I was wondering if anyone could show me how to convert underscores in folder names to spaces using php. I am using the aforementioned code to display a list of folders that contain photo galleries. Of course, the folder names cannot contain spaces. However, when displaying the folder names as links, I would love to know if there's a way to replace the underscores with spaces.

For instance, a folder named: Asia_2000 would be Asia 2000 or Friends_and_Family would be Friends and Family

Anyone know how to do this?
  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Hi,

Sorry about the wrong credit info. :oops:

As I checked on a windows machine, this code does not show underscore instead of space. Is it a unix case? At any rate, this can be easily solved by using ereg or preg to replace all underscores with space before the folder and file names are echoed to the screen.

Another thing I found out, as it is, the code cannot link correctly to files outside of the directory the script is in. In that case the path information does nothing. Or is it a windows problem also?
  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Here is a little example of matching the _ pattern and replace with space.

PHP Code: [ Select ]
$string = "I_am_the_master_of_the_universe";
 
$string = ereg_replace("_", " ", $string);
 
echo $string;
  1. $string = "I_am_the_master_of_the_universe";
  2.  
  3. $string = ereg_replace("_", " ", $string);
  4.  
  5. echo $string;


This would output: I am the master of the universe. :)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Photonomad, Please remember to use either CODE or PHP tags when posting code. I fixed it for you this time :D
  • dsharpe
  • Born
  • Born
  • dsharpe
  • Posts: 2

Post 3+ Months Ago

I am looking for a similar script as discussed her. I am looking to list the contents of a folder. I would like the list to display Page Title and Pages description, pulling info from the pages meta tags. Any ideas?


David
  • X-Rayden
  • Born
  • Born
  • X-Rayden
  • Posts: 2
  • Loc: Beauce, Quebec, Canada

Post 3+ Months Ago

i know its kind of a old subject, but i'd like to give you the twist i gave to this code :

PHP Code: [ Select ]
<?PHP
 
         
 
    /**  
 
     * Change the path to your folder.  
 
     *  
 
     * This must be the full path from the root of your  
 
     * web space. If you're not sure what it is, ask your host.  
 
     *  
 
     * Name this file index.php and place in the directory.  
 
     */  
 
                             
 
     // Define the full path to your folder from root  
 
    $path = "full path to folder(like /doc/doc or ../../doc/doc)";  
 
                             
 
     // Open the folder  
 
    $dir_handle = @opendir($path) or die("Impossible d'ouvrir le dossier $path");  
 
                             
 
    // Loop through the files  
 
    while ($file = readdir($dir_handle)) {  
 
                             
 
    if($file == "." || $file == ".." || $file == "index.php" || substr($file,-3) == "txt" )  
 
                             
 
    continue;
 
   
 
   $fichierexp = $path."/".$file.".txt";
 
   $TheLinkedFile = $path."/".$file;
 
   if(file_exists($fichierexp)) {
 
      @$fp = fopen($fichierexp,'r');
 
      echo "<a href=\"$TheLinkedFile\">".fgets($fp,999)."</a><br>";
 
      fclose($fp);  
 
     } else {  
 
      echo "<a href=\"$TheLinkedFile\">".substr($file,0,-4)."</a><br>";  
 
     }
 
    }                        
 
    // Close  
 
    closedir($dir_handle);                      
 
?>
  1. <?PHP
  2.  
  3.          
  4.  
  5.     /**  
  6.  
  7.      * Change the path to your folder.  
  8.  
  9.      *  
  10.  
  11.      * This must be the full path from the root of your  
  12.  
  13.      * web space. If you're not sure what it is, ask your host.  
  14.  
  15.      *  
  16.  
  17.      * Name this file index.php and place in the directory.  
  18.  
  19.      */  
  20.  
  21.                              
  22.  
  23.      // Define the full path to your folder from root  
  24.  
  25.     $path = "full path to folder(like /doc/doc or ../../doc/doc)";  
  26.  
  27.                              
  28.  
  29.      // Open the folder  
  30.  
  31.     $dir_handle = @opendir($path) or die("Impossible d'ouvrir le dossier $path");  
  32.  
  33.                              
  34.  
  35.     // Loop through the files  
  36.  
  37.     while ($file = readdir($dir_handle)) {  
  38.  
  39.                              
  40.  
  41.     if($file == "." || $file == ".." || $file == "index.php" || substr($file,-3) == "txt" )  
  42.  
  43.                              
  44.  
  45.     continue;
  46.  
  47.    
  48.  
  49.    $fichierexp = $path."/".$file.".txt";
  50.  
  51.    $TheLinkedFile = $path."/".$file;
  52.  
  53.    if(file_exists($fichierexp)) {
  54.  
  55.       @$fp = fopen($fichierexp,'r');
  56.  
  57.       echo "<a href=\"$TheLinkedFile\">".fgets($fp,999)."</a><br>";
  58.  
  59.       fclose($fp);  
  60.  
  61.      } else {  
  62.  
  63.       echo "<a href=\"$TheLinkedFile\">".substr($file,0,-4)."</a><br>";  
  64.  
  65.      }
  66.  
  67.     }                        
  68.  
  69.     // Close  
  70.  
  71.     closedir($dir_handle);                      
  72.  
  73. ?>

i didn't give him the way to change _ for spaces ... but better in a way, it'll echo a true linked to the file (i don't know but the other code didn't worked well with some folder paths) but without the extension... (ex : without .wmv) AND if you do a txt file of the name of the file (if you have hptdoc768.doc and hptdoc768.doc.txt) it'll not echo the txt file, and the first line of the txt file will be the echoed name for the file... i give it this twist, because it's easyer to do a "folder where anybody can throw their file with an explanation" and avoid some ridiculous file name like : this_file_is_the_one_you_should_have_if_your_interested_in_this_compagny.doc
  • Krokman
  • Born
  • Born
  • Krokman
  • Posts: 1

Post 3+ Months Ago

Great Script Thanks

How di I firstly amend this script to open links in a new window and also display them with an icon if its a PDF or a word doc or an xl spreadsheet

Regards

krokman

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 19 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.