PHP - Function file_get_contents()



string file_get_contents ( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen]]]] );

Definition and Usage

This function is identical to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes.


Parameter Description
filename Name of the file to read.

The value of flags can be any combination of the following flags joined with the binary OR (|) operator.

  • FILE_USE_INCLUDE_PATH − Search for filename in the include directory.

  • FILE_TEXT − If unicode semantics are enabled, the default encoding of the read data is UTF-8. his flag cannot be used with FILE_BINARY.

  • FILE_BINARY − With this flag, the file is read in binary mode. This is the default setting and cannot be used with FILE_TEXT.

context A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.
offset The offset where the reading starts.
maxlen Maximum length of data read.

Return Value

The function returns the read data or FALSE on failure.


Following is the usage of this function −

   $lines = file_get_contents('');
   # This will read complete index.htm file.
   echo $lines;