PHP readfile Vs get_file_contents :– In PHP file handling system there are two popular methods to read a file data.
- readfile()
- get_file_contents()
Here are going to discuss both the functions seperately with examples and their respective output screenshots
which you can use on your local computer to understand both the methods in a simple way.
file_get_contents(filename,use_include_path,offset,maxlen)
file_get_contents method is used to read a file contents depending upon your wish as you need in your program.
There are five parameters which we define while calling this function.These parameters are discussed below in detail.
filename
Name of the file to read.
use_include_path
This is a flag and will accept boolean values. If it is TRUE, then the value of FILE_USE_INCLUDE_PATH, PHP constant will be used while searching the file. In recent versions of PHP 5, we should specify FILE_USE_INCLUDE_PATH instead of boolean TRUE.
context
A valid context resource created with stream_context_create(). If you don’t need to use a custom context, you can skip this parameter by NULL.
offset
The offset where the reading starts on the original stream. Negative offsets count from the end of the stream.
Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.
maxlen
Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.
PHP File Handling | Example
Example // readfile() Method
<?php $get_contents=readfile("demo.txt"); echo $get_contents; ??> |
Example // file_get_contents() Method
<?php echo file_get_contents("demo.txt",false,NULL,15,20); ??> |