SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Displaying files in a directory with PHP

    Good afternoon everyone,

    I need to display files in a directory and have the following code, which works great:

    <?php
    $dir = "documents/";
    $dh = opendir($dir);
    while (($file = readdir($dh)) !== false) {
    echo '<a href="documents/'.$file.'">'.$file.'</a><br />';
    }
    closedir($dh);
    ?>

    The results I get is:

    De Beer 01.doc
    De Beer 02.doc
    .
    ..
    Engelbrecht 01.doc

    Can anyone please tell me how to remove the . and .. ?

    Any help would be much appreciated.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try this:


    PHP Code:

    <?php 
    function read_dir($dir$array = array())

      
    $dh opendir($dir); 
      
    $files = array(); 
      while ((
    $file readdir($dh)) !== false)
       { 
           
    $flag false
          if(
    $file !== '.' && $file !== '..' && !in_array($file$array))
          { 
            
    $files[] = $file
          } 
       } 
      return 
    $files
     } 

    ?>
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for replying John, but it is still not working... any other ideas? I don't know what else to try.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    What is not working? Please keep everyone up-to-date with the code that you're trying, so we can explain why it might not be working.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am very new to PHP Salathe, so I have tried a couple of things. This being the last:

    <?php
    function read_dir($dir, $array = array())
    {
    $dir = "documents/";
    $dh = opendir($dir);
    while (($file = readdir($dh)) !== false) {
    $flag = false;
    if($file !== '.' && $file !== '..' && !in_array($file, $array))
    {
    $files[] = $file;
    }
    }
    return $files;
    }
    echo '<a href="documents/'.$file.'">'.$file.'</a><br />';
    }
    closedir($dh);
    ?>

    The output is still:

    De Beer 01.doc
    De Beer 02.doc
    .
    ..
    Engelbrecht 01.doc

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by susanv View Post
    Thanks for replying John, but it is still not working... any other ideas? I don't know what else to try.
    I think your problem was that you were still using your script and not calling the function read_dir()

    Try this and once working delete or remark "echo jj,":

    PHP Code:

    #debug stuff - saves typing
    define('jj''<br />DEBUG: ');
    define('jk'' -  ');

    echo 
    jj__LINE__jk'START';

    echo 
    jj__LINE__jk,
    $dir  "documents/";

    # make sure directory exists to prevent endless loop
    if( is_dir$dir ) )
    {
    echo 
    jj__LINE__jk,
      
    $dh   opendir($dir); 
      
      while ((
    $file readdir($dh)) !== false)
      {
        if( 
    $file === '.'   OR   $file === '..')
        {
          
    # echo '<br />DO NOT ECHO: ' .$file;
        
    }
        else
        {
          echo 
    '<br /><a href="documents/'.$file.'">'.$file.'</a>';
        }     
      }
    }
       
    echo 
    jj__LINE__jk'FINISH'
    Last edited by John_Betong; Mar 25, 2012 at 18:42. Reason: added __LINE__
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Are you really using a version of PHP below 5?
    If using a good version (PHP 5+) then:
    PHP Code:
    $files scandir'/path/to/dir/' );
    foreach ( 
    $files as $file ) {
      if ( 
    $file === '.' || $file === '..' )
        continue;

      echo 
    $file;
    }

    # Or
    $files array_diffscandir$dir ), array( '..''.' ) );
    foreach ( 
    $files as $file )
      echo 
    $file
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Member
    Join Date
    Apr 2011
    Location
    South Africa
    Posts
    24
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much John, that worked!!


Bookmarks

Posting Permissions

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