Results 1 to 7 of 7

Thread: Displaying directories and files using PHP

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Displaying directories and files using PHP

    Right heres a good one, so good i dont know where to start :P

    Just like Apache, i want to be able to display the directories and files of my webserver using PHP.

    Examples in the images attached
    Attached Images Attached Images   

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Displaying directories and files using PHP

    this is basically what you can do. there's plenty of things you can do to make it able to browse other directories, I could give you an old script I made to look at if you like.

    PHP Code:
    <?php
      $dir 
    "./pictures/";
      echo 
    '<h1>' $dir '</h1>' "\n<pre>";
      if(
    is_dir($dir)){
        if(
    $dh opendir($dir)){
          while((
    $file readdir($dh)) !== false){
            
    $nfile $dir $file;
            echo 
    '  -> <a href="' $nfile '">$file</a>' "\n";
          }
        }
      }
      echo 
    '</pre>';
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Displaying directories and files using PHP

    wow works like a charm, however is there one to list files aswell?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Displaying directories and files using PHP

    it already does list files. it lists all of the files and directories in $dir.

    you can check if a file returned by the above code is a directory by using is_dir() (usage: is_dir(filepath)), and then do something else to it. to get a file or directory's last date modified you can use the filemtime() function.

    edit from here on -->

    oh, and oops: I forgot to escape the $file variable in the code I posted. If you didn't already figure it out, you should change it to this: (I also modified the code a bit to show you distinct directories/files)
    PHP Code:
    <?php
      $dir 
    "./";
      
    $dirs "";
      
    $files "";
      if(
    is_dir($dir)){
        if(
    $dh opendir($dir)){
          while((
    $file readdir($dh)) !== false){
            
    $nfile $dir $file;
            if(
    is_dir($nfile)){
              
    $dirs .= '  -> DIR: <a href="' $nfile '">' $file '</a>' "\n";
            }else{
              
    $files .= '  -> FILE: <a href="' $nfile '">' $file '</a>' "\n";
            }
          }
        }
      }
      
    //output
      
    echo "<h1>$dir</h1>\n";
      echo 
    "<h3>directories:</h3>\n<blockquote><pre>\n";
      echo 
    $dirs;
      echo 
    "</pre></blockquote>\n<h3>files:</h3>\n<blockquote><pre>\n";
      echo 
    $files;
      echo 
    "</pre></blockquote>\n";
    ?>
    this way, all directories are displayed first (logically like Apache does) and all files are displayed last. might be a bit slower than the original because you are storing them in variables and then flushing, but otherwise you can't really sort them. When I do this, I usually trap files in an array so that I can sort them alphabetically or using other array sort functions.
    Last edited by kows; Oct 25th, 2006 at 05:15 PM.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Displaying directories and files using PHP

    Um, why not just use Apache's indexing? Would be a damn sight more efficient.

    Also, never use echo() to output HTML code.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Displaying directories and files using PHP

    you usually create a custom indexing thing to be able to do things the default apache one isn't really capable of. if you're just listing out every file, then yes, it's more efficient to use apache's. however, if you're building a script that, for example, listed out all of the PHP files in a given directory and let you view the source (without making phps or txt files, just using fopen()), it would be a lot easier to have your own indexing system that looked at the file extension and printed it only if it was something you wanted.

    Quote Originally Posted by penagate
    never use echo() to output HTML code.
    and why is that?
    Last edited by kows; Oct 27th, 2006 at 02:48 PM.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Displaying directories and files using PHP

    Quote Originally Posted by kows
    you usually create a custom indexing thing to be able to do things the default apache one isn't really capable of.
    Well duh, but he said "just like Apache".

    and why is that?
    PHP is an embedded language, not the other way round; it goes within the HTML code. Output is what PHP does by default. Using echo() to output instead of just closing the PHP tags is unnecessarily inefficient. Also, you make it harder to spot syntactic errors in your HTML, and you have escape certain characters; it's ugly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width