Reading the contents of a file is simple with this function. Just give it the path and you’re done. There are optional parameters too, of course.

Firstly, you can use the following flags as a second parameter in PHP 6.0. For earlier versions, true and FILE_USE_INCLUDE_PATH are equivalent here.

FILE_USE_INCLUDE_PATH: Search for the file in the include directory.
FILE_TEXT (since PHP 6.0): Data is read in text mode.
FILE_BINARY (since PHP 6.0): Data is read in binary mode.

The third parameter is where you’d pass in your stream context.

The fourth and fifth parameters allow you to get only a portion of the file - offset (where to start) and length (how much max to get) in bytes.

For this example, we’ll use the file created in our first file_put_contents() example.

All the laddies cat call and wolf whistle
So-called gentlemen and ladies
Dog fight like rose and thistle
$file = 'output.txt';
 
$data = file_get_contents( $file );
/*
All the laddies cat call and wolf whistle
So-called gentlemen and ladies
Dog fight like rose and thistle
*/
 
$data = file_get_contents( $file, null, null, 5, 17 );
/*
he laddies cat ca
*/