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.


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


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:

$fh=fopen($filename," r ");
echo fread($fh," 10 ");


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


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.


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.


$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 />';


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"


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


String fgetc(resource handle)


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


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


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


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


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.


Array file(string filename)


// 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 All Rights Reserved.