<?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
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-directory.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1288.zip