<?php
declare(strict_types=1);
$cases = [];
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php_ref_fs_' . bin2hex(random_bytes(6));
mkdir($tmpDir, 0777, true);
try {
// Case 1: file_put_contents, file_get_contents, filesize, pathinfo, realpath, is_file/is_dir
$notePath = $tmpDir . DIRECTORY_SEPARATOR . 'sample.txt';
$bytesWritten = file_put_contents($notePath, "Hello PHP FS\n");
$readBack = file_get_contents($notePath);
$info = pathinfo($notePath);
$real = realpath($notePath);
$actual1 = [
'bytes_written' => $bytesWritten,
'filesize' => filesize($notePath),
'content' => $readBack,
'basename' => $info['basename'],
'extension' => $info['extension'],
'is_file' => is_file($notePath),
'is_dir' => is_dir($tmpDir),
'realpath_ends_with_file' => is_string($real) && str_ends_with($real, 'sample.txt'),
];
$expected1 = [
'bytes_written' => 13,
'filesize' => 13,
'content' => "Hello PHP FS\n",
'basename' => 'sample.txt',
'extension' => 'txt',
'is_file' => true,
'is_dir' => true,
'realpath_ends_with_file' => true,
];
$cases[] = [
'name' => 'file_put_and_get_contents_metadata',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: fopen, fwrite, ftell, rewind, fseek, fread, fputcsv, fgetcsv, fclose
$streamPath = $tmpDir . DIRECTORY_SEPARATOR . 'stream.dat';
$fp = fopen($streamPath, 'wb+');
fwrite($fp, 'ABCDEFGH');
$posAfterWrite = ftell($fp);
fseek($fp, 2, SEEK_SET);
$slice = fread($fp, 4);
rewind($fp);
ftruncate($fp, 4);
$truncated = fread($fp, 10);
fclose($fp);
$csvPath = $tmpDir . DIRECTORY_SEPARATOR . 'rows.csv';
$cfp = fopen($csvPath, 'wb+');
fputcsv($cfp, ['id', 'lang'], ',', '"', '');
fputcsv($cfp, ['1', 'PHP 8.4'], ',', '"', '');
rewind($cfp);
$csvRows = [];
while (($row = fgetcsv($cfp, 0, ',', '"', '')) !== false) {
$csvRows[] = $row;
}
fclose($cfp);
$actual2 = [
'pos_after_write' => $posAfterWrite,
'seek_read_slice' => $slice,
'after_truncate' => $truncated,
'csv_rows' => $csvRows,
];
$expected2 = [
'pos_after_write' => 8,
'seek_read_slice' => 'CDEF',
'after_truncate' => 'ABCD',
'csv_rows' => [['id', 'lang'], ['1', 'PHP 8.4']],
];
$cases[] = [
'name' => 'fopen_fwrite_fseek_fread_csv',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: copy, rename, glob, parse_ini_string, unlink
$copyPath = $tmpDir . DIRECTORY_SEPARATOR . 'copy.txt';
$renamedPath = $tmpDir . DIRECTORY_SEPARATOR . 'renamed.txt';
$copyOk = copy($notePath, $copyPath);
$renameOk = rename($copyPath, $renamedPath);
$txtFiles = array_map('basename', glob($tmpDir . DIRECTORY_SEPARATOR . '*.txt'));
sort($txtFiles);
$iniParsed = parse_ini_string("app = mbsky\nport = 8080\n", false, INI_SCANNER_TYPED);
$unlinkOk = unlink($renamedPath);
$actual3 = [
'copy_and_rename' => $copyOk && $renameOk,
'glob_txt' => $txtFiles,
'ini_parsed' => $iniParsed,
'unlinked_gone' => $unlinkOk && !file_exists($renamedPath),
];
$expected3 = [
'copy_and_rename' => true,
'glob_txt' => ['renamed.txt', 'sample.txt'],
'ini_parsed' => ['app' => 'mbsky', 'port' => 8080],
'unlinked_gone' => true,
];
$cases[] = [
'name' => 'copy_rename_glob_parse_ini_unlink',
'passed' => $actual3 === $expected3,
'expected' => $expected3,
'actual' => $actual3,
];
} finally {
foreach (glob($tmpDir . DIRECTORY_SEPARATOR . '*') ?: [] as $f) {
@unlink($f);
}
@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-filesystem-lab/explore-ref-filesystem.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-filesystem.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1290.zip