faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 47 people (83%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do i get a list of filenames from a directory?

Jul 22nd, 2002 21:28
Erich Kolb, Nick Carter, http://www.php.net/manual/en/function.readdir.php


<?php
if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) { 
        echo "$file\n";
    }
    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) { 
        echo "$file\n";
    }
    closedir($handle); 
}
?>