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:
1 2 3 4 5 6 7 |
file_get_contents ( string $filename , bool $use_include_path = false , resource $context = ? , int $start = 0 , int $maxlen = ? ) : string|false |
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.
1 2 3 4 5 6 |
<?php $html = file_get_contents('https://www.example.com/'); echo $html; ?> |
Output:
Read an entire file into a string by PHP file_get_contents() function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php //put file path or any URL $filename = 'readme.txt'; //remove this condition if you use URL to read . if (!is_readable($filename)) { die('File does not exist or is not readable'); } echo file_get_contents($filename); ?> |
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.
Read, only a specific part of a file into a string
1 2 3 4 5 6 7 8 9 |
<?php //put file path or any URL $filename = "http://txt2html.sourceforge.net/sample.txt"; $html = file_get_contents($filename, null, null, 10, 200); echo $html; ?> |
Output:
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 (codingtasks.net).
Also please let me know if you are facing any issues at the time of implementation.
Happy Coding..!
[…] PHP file_get_contents() Function With Example September 15, 2021 […]
[…] PHP file_get_contents() Function With Example September 15, 2021 […]