Forum und email

readdir

(PHP 4, PHP 5)

readdir — Read entry from directory handle

說明

string readdir ( resource $dir_handle )

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

參數

dir_handle

The directory handle resource previously opened with opendir().

Return值

Returns the filename on success, or FALSE on failure.

Warning

本函式可能回傳布林值 FALSE,但也可能回傳一個與 FALSE 等值的非布林值,例如 0 或者 ""。請參閱布林類型章節以獲取更多訊息。應使用 === 運算符來測試本函式的回傳值。

範例

Example#1 List all files in a directory

Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").

<?php
// Note that !== did not exist until 4.0.0-RC2

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);
}
?>

Example#2 List all files in the current directory and strip out . and ..

<?php
if ($handle opendir('.')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
            echo 
"$file\n";
        }
    }
    
closedir($handle);
}
?>

參見