蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 PHP 8.4 scandir、opendir/readdir/rewinddir/closedir、dir() 与 chdir/getcwd

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-ref-directory.php
explore-ref-directory.php108 行
<?php
declare(strict_types=1);

$cases = [];
$origCwd = getcwd();
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php_ref_dir_' . bin2hex(random_bytes(6));
mkdir($tmpDir, 0777, true);
mkdir($tmpDir . DIRECTORY_SEPARATOR . 'subdir', 0777, true);
file_put_contents($tmpDir . DIRECTORY_SEPARATOR . 'alpha.txt', 'A');
file_put_contents($tmpDir . DIRECTORY_SEPARATOR . 'beta.log', 'B');

try {
    // Case 1: scandir() ascending and descending + getcwd()
    $asc = scandir($tmpDir, SCANDIR_SORT_ASCENDING);
    $desc = scandir($tmpDir, SCANDIR_SORT_DESCENDING);

    $actual1 = [
        'getcwd_non_empty' => is_string($origCwd) && $origCwd !== '',
        'scandir_asc' => $asc,
        'scandir_desc' => $desc,
    ];
    $expected1 = [
        'getcwd_non_empty' => true,
        'scandir_asc' => ['.', '..', 'alpha.txt', 'beta.log', 'subdir'],
        'scandir_desc' => ['subdir', 'beta.log', 'alpha.txt', '..', '.'],
    ];
    $cases[] = [
        'name' => 'scandir_and_getcwd',
        'passed' => $actual1 === $expected1,
        'expected' => $expected1,
        'actual' => $actual1,
    ];

    // Case 2: opendir(), readdir(), rewinddir(), closedir()
    $handle = opendir($tmpDir);
    $entries = [];
    while (($entry = readdir($handle)) !== false) {
        $entries[] = $entry;
    }
    $firstPassFirst = $entries[0] ?? null;
    rewinddir($handle);
    $afterRewindFirst = readdir($handle);
    closedir($handle);
    sort($entries);

    $actual2 = [
        'sorted_entries' => $entries,
        'rewind_matches_first' => $firstPassFirst === $afterRewindFirst,
        'entry_count' => count($entries),
    ];
    $expected2 = [
        'sorted_entries' => ['.', '..', 'alpha.txt', 'beta.log', 'subdir'],
        'rewind_matches_first' => true,
        'entry_count' => 5,
    ];
    $cases[] = [
        'name' => 'opendir_readdir_rewinddir_closedir',
        'passed' => $actual2 === $expected2,
        'expected' => $expected2,
        'actual' => $actual2,
    ];

    // Case 3: dir() Directory class + chdir() and restore
    $d = dir($tmpDir);
    $isDirObj = $d instanceof Directory;
    $dirEntries = [];
    while (($item = $d->read()) !== false) {
        if ($item !== '.' && $item !== '..') {
            $dirEntries[] = $item;
        }
    }
    $d->rewind();
    $rewoundItem = $d->read();
    $d->close();
    sort($dirEntries);

    $chdirOk = chdir($tmpDir);
    $movedMatches = realpath(getcwd()) === realpath($tmpDir);
    $restoreOk = chdir($origCwd);

    $actual3 = [
        'is_directory_instance' => $isDirObj,
        'filtered_entries' => $dirEntries,
        'rewind_non_false' => $rewoundItem !== false,
        'chdir_and_restore' => $chdirOk && $movedMatches && $restoreOk,
    ];
    $expected3 = [
        'is_directory_instance' => true,
        'filtered_entries' => ['alpha.txt', 'beta.log', 'subdir'],
        'rewind_non_false' => true,
        'chdir_and_restore' => true,
    ];
    $cases[] = [
        'name' => 'dir_class_and_chdir_restore',
        'passed' => $actual3 === $expected3,
        'expected' => $expected3,
        'actual' => $actual3,
    ];
} finally {
    chdir($origCwd);
    @unlink($tmpDir . DIRECTORY_SEPARATOR . 'alpha.txt');
    @unlink($tmpDir . DIRECTORY_SEPARATOR . 'beta.log');
    @rmdir($tmpDir . DIRECTORY_SEPARATOR . 'subdir');
    @rmdir($tmpDir);
}

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-ref-directory-lab/explore-ref-directory.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-ref-directory.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1288.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习