Open In App

What is the difference between file_get_contents() and file_put_contents() functions in PHP?

Last Updated : 31 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

file_get_contents() Function: The file_get_contents() function is used to read a file into a string. It uses memory mapping techniques that are supported by the server and thus enhances the performance by making it a preferred way of reading the 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.  

  • $path: It specifies the path of the file or directory you want to check.
  • $include_path: It is an optional parameter that searches for a file in the file in the include_path (in php.ini) also if it is set to 1.
  • $context: It is an optional parameter that is used to specify a custom context.
  • $start: It is an optional parameter that is used to specify the starting point in the file for reading.
  • $max_length: It is an optional parameter that 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 Exceptions: If you want to open a file with special characters, such as spaces, it needs to be encoded first using PHP urlencode().
The file_get_contents() function returns boolean FALSE, but may also return a non-boolean value which evaluates to FALSE.
An E_WARNING level error is generated if the filename cannot be found, $maxlength is less than zero, or if seeking the specified offset in the stream fails.

Example: This example illustrates getting the file into a string.We will use the file_get_contents() function. We will specify the URL link as an argument that will redirect to the given site. 

PHP




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


gfg.txt: The following is the content for this file used in all the examples.

A computer science portal for geeks

Output:

A computer science portal for geeks

file_put_contents() Function: The file_put_contents() function in PHP is an inbuilt function that is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn’t exist, it creates a new file.

The path of the file on which the user wants to write and the data that has to be written are sent as parameters to the function and it returns the number of bytes that were written on the file on success and FALSE on failure.

Syntax:

file_put_contents($file, $data, $mode, $context)

Parameters: The file_put_contents() function in PHP accepts two mandatory parameters and two optional parameters.

  • $file: It specifies the file in which you want to write.
  • $data: It specifies the data that has to be written on the file. It can be a string, an array, or a data stream.
  • $context: It is an optional parameter that is used to specify a custom context or the behavior of the stream.
  • $mode: It is an optional parameter that is used to specify how the data has to be written on the file such as FILE_USE_INCLUDE_PATH, FILE_APPEND, LOCK_EX.

Return Value: It returns the number of bytes that were written on the file on success and FALSE on failure.

Errors And Exceptions: The file_put_contents() function returns Boolean FALSE, but may also return a non-boolean value which evaluates to FALSE. This function fails to write contents if the directory provided is invalid.

Examples:

Input : file_put_contents("gfg.txt", "A computer 
                    science portal for geeks!");
Output : 36

Input : $file_pointer = 'gfg.txt';
        $open = file_get_contents($file_pointer);
        $open .= "A computer science portal for geeks!";
        file_put_contents($file_pointer, $open);
Output : 36

Example 1: Below programs illustrate the file_put_contents() function.

PHP




<?php
 
// Writing content on gfg.txt
echo file_put_contents("gfg.txt",
    "A computer science portal for geeks!");
?>


Output:

36

Example 2:

PHP




<?php
 
$file_pointer = 'gfg.txt';
 
// Open the file to get existing content
$open = file_get_contents($file_pointer);
 
// Append a new person to the file
$open .= "A computer science portal for geeks!";
 
// Write the contents back to the file
file_put_contents($file_pointer, $open);
 
?>


Output:

36

Difference between file_get_contents() and file_put_contents() Functions: The main difference between file_get_contents() and file_put_contents() functions is that file_get_contents() function reads a file into a string and file_put_contents() function writes a string to a file.

Let us see the differences in a tabular form -:

   file_get_contents() file_put_contents()
1. The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.
2.

Its syntax is -:

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

Its syntax is -:

file_put_contents(filename, data, mode, context)

3. Its return value is string otherwise it returns false on failure. Its return value is number of bytes written into the file on success otherwise it returns false.
4. It is Binary Safe. It is Binary Safe.
5. It is supported in PHP version 4.3+ It is supported in PHP versions 5.0+


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads