PHP File fread fgets fgetc file get contents


The fread(), fgets(), fgetc(), file_get_contents() and file() functions are used to read content from a file.


fread()

The fread() function is used to read a file(which can be safely used as a binary file).

grammar:

String fread(int handle, int length)

fread() Reads up to length bytes from the file pointer handle. The file is stopped when any of the following conditions are encountered:

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

Reads 10 bytes(including spaces) from the file:

$filename="test.txt";
$fh=fopen($filename," r ");
echo fread($fh," 10 ");
fclose($fh);

Tip

if you just want to read the contents of a file into a string, you should use better-performingfile_get_contents">.


fgets()

The fgets() function is used to read thelinedata from the file and point the file pointer to the next line.

Tip: Use the fgetss() function if you want to remove the HTML markup in your file when reading it.

grammar:

String fgets(int handle[, int length])

fgets() reads a line from the file pointed to by handle and returns a string of up to length-1 bytes. EOF or has read length-1 bytes after a line break(including in the return value). if you do not specify length, the default is 1K, or 1024 bytes.

example:

$fh=@fopen("test.txt"," r ") or die("open test.txt file error! ");
// if condition Avoid invalid pointer
if ($fh) {
    while(!feof($fh)) {
        echo fgets($fh), '<br />';
    }
}
fclose($fh);

Supplement

feof() function to test whether the file pointer to the end of the file location, the file pointer must be effective, if the resource is invalid, it will fall into an infinite loop. See "PHP File Pointer Functions"


fgetc()

The fgetc() function is used to read file dataverbatimuntil the end of the file.

grammar:

String fgetc(resource handle)

example:

$fh=@fopen("test.txt"," r ") or die("open test.txt file error! ");
if ($fh) {
    while(!feof($fh)) {
        echo fgetc($fh);
    }
}
fclose($fh);

file_get_contents()

The file_get_contents() function is used to readentire fileinto a string and return a string if successful, or FALSE if it fails.

grammar:

String file_get_contents(string filename[, int offset[, int maxlen]])
Parameter Description:
parameter Description
filename the name of the file to read
offset Optional, specify the location where the reading begins, default to the start of the file
maxlen Optional, specifies the length of the read file, in bytes

example:

// When reading, the coworker converts the line break to <br />
echo nl2br(file_get_contents('test.txt'));

file()

The file() function is used to read theentire fileinto an array. Each element in the array is the corresponding line in the file, including the newline. Returns an array if successful, or FALSE if it fails.

grammar:

Array file(string filename)

example:

$lines=file('test.txt');
// Loop through the array and add the line number
foreach($lines as $line_num => $line) {
    echo "Line # {$line_num}:", $line, '<br />';
}

Test.txt file content:

Hello there!
This is the second line of text.

The browser displays:

Line # 0: Hi!
Line # 1: This is the second line of text.



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook