Home  >  Article  >  php教程  >  PHP file reading fread, fgets, fgetc, file_get_contents and file functions

PHP file reading fread, fgets, fgetc, file_get_contents and file functions

WBOY
WBOYOriginal
2016-10-28 15:03:252016browse

fread(), fgets(), fgetc(), file_get_contents() and file() functions are used to read contents from files.

fread()

fread() function is used to read files (safe for binary files).
Grammar:

string fread( 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:

  • After reading up to length bytes
  • When the end of file is reached (EOF)
  • (for network streams) when a package is available
  • or (after opening the userspace stream) when 8192 bytes have been read

Read 10 bytes (including spaces) from a file:

php
// http://www.manongjc.com/article/1346.html
$filename = "test.txt";
$fh = fopen($filename, "r");
echo fread($fh, "10");
fclose($fh);
?>

Tips

If you just want to read the contents of a file into a string, you should use file_get_contents() with better performance.

fgets()

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:

string fgets( 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:

php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
// if条件避免无效指针
// http://www.manongjc.com/article/1347.html
if($fh){
    while(!feof($fh)) {
        echo fgets($fh), '
'; } } fclose($fh); ?>

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"

fgetc()

The fgetc() function is used to read file data word by word until the end of the file.
Grammar:

string fgetc( resource handle )

Example:

php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
// http://www.manongjc.com/article/1348.html
if($fh){
    while(!feof($fh)) {
        echo fgetc($fh);
    }
}
fclose($fh);
?>

file_get_contents()

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:

string file_get_contents( string 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:

php
// 读取时同事将换行符转换成 
echo nl2br(file_get_contents('test.txt')); ?>

file()

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:

array file( string filename )

Example:

php
$lines = file('test.txt');
// 在数组中循环并加上行号
// http://www.manongjc.com/article/1349.html
foreach ($lines as $line_num => $line) {
    echo "Line #{$line_num} : ",$line,'
'; } ?>

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn