fread(), fgets(), fgetc(), file_get_contents() and file() functions are used to read contents from files.
fread() function is used to read files (safe for binary files).
Grammar:
<span style="color: #0000ff">string</span> <span style="color: #008080">fread</span>( int handle, int length )
fread() reads up to length bytes from the file pointer handle. Reading of files will stop when any of the following conditions are encountered:
Read 10 bytes (including spaces) from a file:
<?<span style="color: #000000">php </span><span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1346.html</span> <span style="color: #800080">$filename</span> = "test.txt"<span style="color: #000000">; </span><span style="color: #800080">$fh</span> = <span style="color: #008080">fopen</span>(<span style="color: #800080">$filename</span>, "r"<span style="color: #000000">); </span><span style="color: #0000ff">echo</span> <span style="color: #008080">fread</span>(<span style="color: #800080">$fh</span>, "10"<span style="color: #000000">); </span><span style="color: #008080">fclose</span>(<span style="color: #800080">$fh</span><span style="color: #000000">); </span>?>
Tips
If you just want to read the contents of a file into a string, you should use file_get_contents() with better performance.
The fgets() function is used to read a line of data from a file and point the file pointer to the next line.
Tips: If you want to remove HTML tags in the file when reading, please use the fgetss() function.
Grammar:
<span style="color: #0000ff">string</span> <span style="color: #008080">fgets</span>( int handle [, int length] )
fgets() reads a line from the file pointed to by handle and returns a string up to length-1 bytes long. Stops on a newline character (included in the return value), EOF, or after length-1 bytes have been read. If length is not specified, it defaults to 1K, or 1024 bytes.
Example:
<?<span style="color: #000000">php </span><span style="color: #800080">$fh</span> = @<span style="color: #008080">fopen</span>("test.txt","r") or <span style="color: #0000ff">die</span>("打开 test.txt 文件出错!"<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000"> if条件避免无效指针 // http://www.manongjc.com/article/1347.html</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$fh</span><span style="color: #000000">){ </span><span style="color: #0000ff">while</span>(!<span style="color: #008080">feof</span>(<span style="color: #800080">$fh</span><span style="color: #000000">)) { </span><span style="color: #0000ff">echo</span> <span style="color: #008080">fgets</span>(<span style="color: #800080">$fh</span>), '<br />'<span style="color: #000000">; } } </span><span style="color: #008080">fclose</span>(<span style="color: #800080">$fh</span><span style="color: #000000">); </span>?>
Additional instructions
feof() function tests whether the file pointer reaches the end of the file. The file pointer must be valid. If it is an invalid resource, it will fall into an infinite loop. See "PHP File Pointer Function"
The fgetc() function is used to read file data word by word until the end of the file.
Grammar:
<span style="color: #0000ff">string</span> <span style="color: #008080">fgetc</span>( <span style="color: #0000ff">resource</span> handle )
Example:
<?<span style="color: #000000">php </span><span style="color: #800080">$fh</span> = @<span style="color: #008080">fopen</span>("test.txt","r") or <span style="color: #0000ff">die</span>("打开 test.txt 文件出错!"<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1348.html</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$fh</span><span style="color: #000000">){ </span><span style="color: #0000ff">while</span>(!<span style="color: #008080">feof</span>(<span style="color: #800080">$fh</span><span style="color: #000000">)) { </span><span style="color: #0000ff">echo</span> <span style="color: #008080">fgetc</span>(<span style="color: #800080">$fh</span><span style="color: #000000">); } } </span><span style="color: #008080">fclose</span>(<span style="color: #800080">$fh</span><span style="color: #000000">); </span>?>
The file_get_contents() function is used to read the entire file into a string, returning a string if successful, and FALSE if failed.
Grammar:
<span style="color: #0000ff">string</span> <span style="color: #008080">file_get_contents</span>( <span style="color: #0000ff">string</span> filename [, int offset [, int maxlen]] )
Parameter Description:
Parameter Description
filename The name of the file to be read
offset Optional, specify the starting position of reading, the default is the starting position of the file
maxlen Optional, specify the length of the read file, in bytes
Example:
<?<span style="color: #000000">php </span><span style="color: #008000">//</span><span style="color: #008000"> 读取时同事将换行符转换成 <br /> <span style="color: #0000ff">echo</span> <span style="color: #008080">nl2br</span>(<span style="color: #008080">file_get_contents</span>('test.txt'<span style="color: #000000">)); </span>?>
The file() function is used to read the entire file into an array. Each unit in the array is a corresponding line in the file, including newlines. Returns an array on success, FALSE on failure.
Grammar:
<span style="color: #0000ff">array</span> <span style="color: #008080">file</span>( <span style="color: #0000ff">string</span> filename )
Example:
<?<span style="color: #000000">php </span><span style="color: #800080">$lines</span> = <span style="color: #008080">file</span>('test.txt'<span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000"> 在数组中循环并加上行号 // http://www.manongjc.com/article/1349.html</span> <span style="color: #0000ff">foreach</span> (<span style="color: #800080">$lines</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$line_num</span> => <span style="color: #800080">$line</span><span style="color: #000000">) { </span><span style="color: #0000ff">echo</span> "Line #{<span style="color: #800080">$line_num</span>} : ",<span style="color: #800080">$line</span>,'<br />'<span style="color: #000000">; } </span>?>
test.txt file content:
Hello!
This is the second line of text.
Browser display:
Line #0: Hello!
Line #1 : This is the second line of text.