温馨提示×

php opendir和closedir配合使用

PHP
小樊
83
2024-07-14 17:27:29
栏目: 编程语言

<?php
$dir = '/path/to/directory';

// Open the directory
$handle = opendir($dir);

// Check if the directory was opened successfully
if ($handle) {
    // Read all files in the directory
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }
    
    // Close the directory
    closedir($handle);
} else {
    echo "Failed to open directory\n";
}
?>

在这个示例中,我们首先使用opendir()函数打开指定目录,然后使用readdir()函数读取目录中的文件,并在控制台输出文件名。最后,我们使用closedir()函数关闭目录句柄。如果目录无法打开,则输出“Failed to open directory”。

0