fopen / feof / fclose 适合文件和网页

最后更新于:2022-04-02 02:29:55

[TOC] ## 示例 ### php://stdout ``` $f = fopen("php://stdout", "w"); fwrite($f,"== hello == \n"); echo "world"; ``` 启用 `php test.php`,可以在web输出 world , 在命令行输出== hello == ### 文件 ``` $handle = fopen("a.txt",'r'); // feof — 测试文件指针是否到了文件结束的位置 while(!feof($handle)){ // 每次读 8字节 $fread = fread($handle, 8); echo $fread.PHP_EOL; } fclose($handle); ``` ### 网页 ``` $fh = fopen("http://www.baidu.com", "r"); if ($fh === false) { exit("Cannot read file\n"); } while(!feof($fh)){ $fgets = fgets($fh); echo $fgets.PHP_EOL; } ```
';