Directory listing in PHP

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

<?
/* Put the full path to the directory you want to read, in a variable called $dir_name */
$dir_name = "/home/isgcgi/www/newsletters";
/* Create a handle, the result of opening the given directory */
$dir = opendir($dir_name);
/* Start a text block, into which you'll place the list elements (your filenames) */
$file_list = "

    \n";
    /* Using a while statement, read all of the elements in the open directory. If the name of the file is not "." or "..", print the name in your list */
    while ($file_name = readdir($dir)) {
    if (($file_name != ".") && ($file_name != "..")) {
    $file_list .= "
  • $file_name
  • \n";
    }
    }
    /* Finish up your bullet list */
    $file_list .= "

\n";
/* Close the open directory handle and end your PHP block*/
closedir($dir);
?>

Directory Listing

Files in: <? echo "$dir_name"; ?>

<? echo "$file_list"; ?>

What am I doing wrong? I can't get the first $filename to read out, it shows as nothing, but the second $filename prints fine.

Adapting the script from http://builder.cnet.com/webbuilding/0-3882-8-4639552-5.html

Help!

Smiling Suzanne

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..") && ($file_name != "index.php")) {
$file_list .= "

  • $file_name
  • \n";
    }

    Make sure the freaking variable is right! Argh.

    Smiling S

    Mark Hensler's picture

    He has: 4,048 posts

    Joined: Aug 2000

    That does help a bit, doesn't it?

    They have: 601 posts

    Joined: Nov 2001

    You could probably optimise that code a fair way, if you wanted to. There's no grep function in PHP?

    Suzanne's picture

    She has: 5,507 posts

    Joined: Feb 2000

    How so?

    I'm intrigued!

    Of course, I'm also just learning (duh!) so talk in small words.

    Smiling Suzanne

    Mark Hensler's picture

    He has: 4,048 posts

    Joined: Aug 2000

    Yes, there are grep functions. PHP doesn't function the same way PERL does with regex. PHP requires you to put your regex in a function (whereas in perl, practically anywhere will do).

    PHP Docs:
    ereg(), ereg_replace(),
    eregi(), eregi_replace()

    LXXXIV. Regular Expression Functions (Perl-Compatible)
    preg_match(), preg_match_all(), preg_replace()

    You don't really need PHP or PERL to filter files...

    <?php
    $command
    = \"ls -w1 \" . $dir . \"/*.htm* | awk 'BEGIN {FS=\\\"/\\\"} {print \\$NF}'\";
    exec(
    $command, $dir_list);

    foreach (
    $dir_list as $file) {
       
    $file_list .= '<li><a href=\"' . $file . '\">$file</a></li>\n';
    }
    ?>
    I'm doing something similar to parse out map files for my WAHL project. I wrote a shell script a while ago to do this kind of stuff, and now I'm copy & pasting it into my PHP. Wink

    Mark Hensler
    If there is no answer on Google, then there is no question.

    Suzanne's picture

    She has: 5,507 posts

    Joined: Feb 2000

    I'm just starting to explore the concept, but I, just today, made an index.php page that read the contents of a directory (all different kinds of images) and wrote code around them. Since there were over 80 images, this saved me a WHACK of time. I had them write the JavaScript for the rollovers, too.

    Very interesting.

    I am seeing all sorts of faster development coming.

    Thanks for the links, Mark, too. My goal was to learn PHP while on maternity leave, but the maternity leave isn't really happening, so I'll have to learn it on the job.

    Smiling Suzanne

    Mark Hensler's picture

    He has: 4,048 posts

    Joined: Aug 2000

    Quote: Originally posted by Suzanne
    My goal was to learn PHP while on maternity leave, but the maternity leave isn't really happening, so I'll have to learn it on the job.

    Doh! Doh!

    Mark Hensler
    If there is no answer on Google, then there is no question.

    Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.