fopen() 的第一个参数包含被打开的文件名,第二个参数规定打开文件的模式。如果 fopen() 函数未能打开指定的文件,下面的例子会生成一段消息:
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?>按行读取,直到文件结束
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // 输出单行直到 end-of-file while(!feof($myfile)) { echo fgets($myfile) . ""; } fclose($myfile); ?>
按字符读取,直到文件结束
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // 输出单字符直到 end-of-file while(!feof($myfile)) { echo fgetc($myfile); } fclose($myfile); ?>