Skip to content
Home » PHP file_get_contents() Function With Example

PHP file_get_contents() Function With Example

In this tutorial, we learn how we can get the complete string from any URL or from any text file by using PHP file_get_contents() Function using multiple examples.

Introduction and Usage

Mainly, the file_get_contents() Function is used to read strings from the entire file.

This is a feasible way to read the complete file into a string. PHP file_get_contents() function using a memory mapping technique to enhance the performance (If supported by the server).

Syntax:

Parameters:

Here are the complete details of the parameters used by the file_get_contents() function.

string $filename Here we define the name of the file which is string type.

bool $use_include_path This parameter helps to take the data from included path inside the root file. (Optional)

$context The context is the set of options that can modify the way the stream is. To ignore it you can use a NULL value. (Optional)

int $start It takes an integer value from where the stream starts and if you set it to negative then it starts from the last. (Optional)

int $maxlen If you set the maximum length it reads only the specified length otherwise complete. (Optional)

Return Values

If a file or URL is accessible and available then it returns complete data as a string or web page otherwise it returns false on failure.

Technical Details

PHP Version:4.3+
Binary Safe:Yes, in PHP 4.3
PHP Changelog:PHP 7.1 – Support for negative values in a start parameter
PHP 5.1 – Added the start and max_length parameters

PHP file_get_contents() Examples

Here we will see 3 examples of the PHP file_get_contents() function,

  • Using the file_get_contents() function to download a webpage from the URL.
  • Read an entire file into a string by PHP file_get_contents() function.
  • Read only a part of a file into a string.

Using the file_get_contents() function to get a webpage from the URL

Here we will just pass the URL as a parameter to get the web page from that URL.

Output:

Using the file_get_contents() function to get a webpage from the URL
Using the file_get_contents() function to get a webpage from the URL

Read an entire file into a string by PHP file_get_contents() function

How it will execute,

First, it checks whether the file is accessible and readable or not, if not then the execution is stopped and prints the error message.

Second, if the file is correct and readable then it prints the complete string from that file.

Also Read: Detailed Article About JavaScript else if Statement

Read, only a specific part of a file into a string

Output:

 Read, only a specific part of a file into a string
Read, only a specific part of a file into a string

In this example, we use the start and maximum length parameters at last. Which use to get specific data from the file.

Conclusion

Here we discuss the complete concept and use of the PHP file_get_contents() function with the help of some running examples.

You can run it by using our editor Run PHP Code (phpcoder.tech).

Also please let me know if you are facing any issues at the time of implementation.

Also Read: JavaScript toLowerCase Function With Examples

Happy Coding..!

Was this article helpful?
YesNo

2 thoughts on “PHP file_get_contents() Function With Example”

  1. Pingback: How to Search and Replace Tag Value in XML File using PHP

  2. Pingback: PHP trim() Function With Multiple Examples - PHPCODER.TECH

Leave a Reply

Your email address will not be published. Required fields are marked *