PHP | file_get_contents() Function

The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.

The path of the file to be read is sent as a parameter to the function and it returns the data read on success and FALSE on failure.

Syntax:



file_get_contents($path, $include_path, $context, 
                              $start, $max_length)

Parameters: The file_get_contents() function in PHP accepts one mandatory parameter and four optional parameters.

  1. $path: It specifies the path of the file or directory you want to check.
  2. $include_path: It is an optional parameter which searches for a file in the file in the include_path (in php.ini) also if it is set to 1.
  3. $context: It is an optional parameter which is used to specify a custom context.
  4. $start: It is an optional parameter which is used to specify the starting point in the file for reading.
  5. $max_length: It is an optional parameter which is used to specify the number of bytes to be read.

Return Value: It returns the read data on success and FALSE on failure.

Errors And Exception:

  1. If you want to open a file with special characters, such as spaces, it needs to be encoded first using urlencode().
  2. The file_get_contents() function returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
  3. An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking to the specified offset in the stream fails.

Examples:

Input:  file_get_contents('https://www.geeksforgeeks.org/');
Output: A computer science portal for geeks

Input:  file_get_contents('gfg.txt', FALSE, NULL, 0, 14);
Output: A computer science portal for geeks

Below programs illustrate the file_get_contents() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// reading contents from the
// geeksforgeeks homepage
$homepage = file_get_contents("https://www.geeks
                                 forgeeks.org/");
echo $homepage;
  
?>

chevron_right


Output:

A computer science portal for geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// reading 36 bytes startig from
// the 0th character from gfg.txt
$text = file_get_contents('gfg.txt',
                  FALSE, NULL, 0, 36);
echo $text;
  
?>

chevron_right


Output:

A computer science portal for geeks

Reference:
http://php.net/manual/en/function.file-get-contents.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.