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 with 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 is the complete details of parameters used by the file_get_contents() function .

string $filename Here we define the name of the file in 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 of the stream. To ignore it you can use 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 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 the file is accessible and readable or not, if not then the execution is stop 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 know if you are facing any issue at the time of implementation.

Also Read: JavaScript toLowerCase Function With Examples

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 175

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

- milanobet giriş - gizabet giriş -
perabet giriş
- betkanyon giriş -

jojobet giriş

We are now accepting guest posting and creation of back-links.
This is default text for notification bar