File_get_contents() is one of the popular file handling functions in PHP. There are other similar file handling functions like read() and file(). However file_get_contents() function has its own advantages among all others.
Index
The primary usage of the file_get_contents() is to read the specified file. It also provides extra features like from where to start and how much to read from the file.
file_get_contents($file_path, $file_include_path, $context, $start_position, $max_length)
Explanation
In the file_get_contents() function, we have to pass the file path as a parameter to read the file, and all other parameters are optional.
<?php
// read contents from the text file
$data = file_get_contents("https://errorsea.com/file.txt");
echo $data;
?>
Line 1: Cause 'error' is the signal of success Line 2: Errorsea is the Best Programming Website
Note: file_get_contents() function can not read line by line.
<?php
// read first 46 characters from the text file
$data = file_get_contents("https://errorsea.com/file.txt", FALSE, NULL, 0, 46);
echo $data;
?>
Line 1: Cause 'error' is the signal of success
Read Also: How to Include File in PHP [All Methods]
There are some special occasions when we have to read files without new lines and from specific positions to a fixed length. At that time file_get_contents() function helps to fulfill our requirement.
I hope you understand the complete usage of the file_get_contents() function.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…