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

运行 fopen 八模式生命周期、fgets 逐行、fgetc 逐字节与错误分支实验

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

$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php_file_lab_' . bin2hex(random_bytes(6));
mkdir($tmpDir, 0777, true);

$cleanup = static function (string $dir): void {
    if (!is_dir($dir)) {
        return;
    }
    foreach (scandir($dir) ?: [] as $item) {
        if ($item === '.' || $item === '..') {
            continue;
        }
        @unlink($dir . DIRECTORY_SEPARATOR . $item);
    }
    @rmdir($dir);
};

$cases = [];

try {
    // Case 1: All 8 fopen() modes (r, r+, w, w+, a, a+, x, x+) and fclose()
    $fMain = $tmpDir . '/main.txt';
    $fpW = fopen($fMain, 'w');
    fwrite($fpW, 'Hello');
    $closedW = fclose($fpW);

    $fpA = fopen($fMain, 'a');
    fwrite($fpA, ' World');
    fclose($fpA);

    $fpR = fopen($fMain, 'r');
    $readAfterA = fread($fpR, 100);
    fclose($fpR);

    $fpRPlus = fopen($fMain, 'r+');
    fwrite($fpRPlus, 'PHP84');
    rewind($fpRPlus);
    $readAfterRPlus = fread($fpRPlus, 100);
    fclose($fpRPlus);

    $fpWPlus = fopen($fMain, 'w+');
    fwrite($fpWPlus, 'Reset');
    rewind($fpWPlus);
    $readAfterWPlus = fread($fpWPlus, 100);
    fclose($fpWPlus);

    $fpAPlus = fopen($fMain, 'a+');
    fwrite($fpAPlus, '+Tail');
    rewind($fpAPlus);
    $readAfterAPlus = fread($fpAPlus, 100);
    fclose($fpAPlus);

    $fExcl = $tmpDir . '/exclusive.txt';
    $fpX = fopen($fExcl, 'x');
    fwrite($fpX, 'NewOnly');
    fclose($fpX);
    $secondXFailed = (@fopen($fExcl, 'x') === false);

    $fExclPlus = $tmpDir . '/exclusive_plus.txt';
    $fpXPlus = fopen($fExclPlus, 'x+');
    fwrite($fpXPlus, 'RWNew');
    rewind($fpXPlus);
    $readXPlus = fread($fpXPlus, 100);
    fclose($fpXPlus);
    $secondXPlusFailed = (@fopen($fExclPlus, 'x+') === false);

    $actual1 = [
        'closed_w' => $closedW,
        'after_w_and_a' => $readAfterA,
        'after_r_plus_overwrite' => $readAfterRPlus,
        'after_w_plus_truncate' => $readAfterWPlus,
        'after_a_plus_append' => $readAfterAPlus,
        'x_rejects_existing' => $secondXFailed,
        'x_plus_read' => $readXPlus,
        'x_plus_rejects_existing' => $secondXPlusFailed,
    ];
    $expected1 = [
        'closed_w' => true,
        'after_w_and_a' => 'Hello World',
        'after_r_plus_overwrite' => 'PHP84 World',
        'after_w_plus_truncate' => 'Reset',
        'after_a_plus_append' => 'Reset+Tail',
        'x_rejects_existing' => true,
        'x_plus_read' => 'RWNew',
        'x_plus_rejects_existing' => true,
    ];
    $cases[] = [
        'name' => 'fopen_modes_rwax_lifecycle',
        'passed' => $actual1 === $expected1,
        'expected' => $expected1,
        'actual' => $actual1,
    ];

    // Case 2: Line-by-line reading with fgets() and feof()
    $fLines = $tmpDir . '/welcome.txt';
    file_put_contents($fLines, "第一行:欢迎\n第二行:PHP 文件处理\n第三行:结束");
    $fp = fopen($fLines, 'r');
    $lines = [];
    while (($line = fgets($fp)) !== false) {
        $lines[] = rtrim($line, "\r\n");
    }
    $atEof = feof($fp);
    fclose($fp);

    $actual2 = [
        'line_count' => count($lines),
        'lines' => $lines,
        'eof_reached' => $atEof,
    ];
    $expected2 = [
        'line_count' => 3,
        'lines' => ['第一行:欢迎', '第二行:PHP 文件处理', '第三行:结束'],
        'eof_reached' => true,
    ];
    $cases[] = [
        'name' => 'fgets_line_by_line_eof',
        'passed' => $actual2 === $expected2,
        'expected' => $expected2,
        'actual' => $actual2,
    ];

    // Case 3: Character-by-character reading with fgetc() and feof()
    $fChars = $tmpDir . '/chars.txt';
    file_put_contents($fChars, "PHP84\nOK");
    $fpC = fopen($fChars, 'r');
    $chars = [];
    while (($ch = fgetc($fpC)) !== false) {
        $chars[] = $ch;
    }
    $eofC = feof($fpC);
    fclose($fpC);

    $actual3 = [
        'char_count' => count($chars),
        'reconstructed' => implode('', $chars),
        'eof_reached' => $eofC,
    ];
    $expected3 = [
        'char_count' => 8,
        'reconstructed' => "PHP84\nOK",
        'eof_reached' => true,
    ];
    $cases[] = [
        'name' => 'fgetc_char_by_char_eof',
        'passed' => $actual3 === $expected3,
        'expected' => $expected3,
        'actual' => $actual3,
    ];

    // Case 4: Missing file returns false from fopen() and write-only mode ('w') cannot be read
    $missingHandle = @fopen($tmpDir . '/missing.txt', 'r');
    $fWriteOnly = $tmpDir . '/writeonly.txt';
    $fpWO = fopen($fWriteOnly, 'w');
    fwrite($fpWO, 'secret');
    rewind($fpWO);
    $readOnWriteOnly = @fread($fpWO, 10);
    fclose($fpWO);

    $actual4 = [
        'missing_returns_false' => $missingHandle === false,
        'read_on_w_mode_empty_or_false' => ($readOnWriteOnly === false || $readOnWriteOnly === ''),
        'actual_written_via_file_get_contents' => file_get_contents($fWriteOnly),
    ];
    $expected4 = [
        'missing_returns_false' => true,
        'read_on_w_mode_empty_or_false' => true,
        'actual_written_via_file_get_contents' => 'secret',
    ];
    $cases[] = [
        'name' => 'fopen_missing_file_false_and_write_only_read',
        'passed' => $actual4 === $expected4,
        'expected' => $expected4,
        'actual' => $actual4,
    ];
} finally {
    $cleanup($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-file-lab/explore-file.php

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

下载课程关联工程包:PHP

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

下载课程关联工程包:PHP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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