For reading file there were two functions available.
- int readfile ( string
$filename
, bool$use_include_path
= false , resource$context
)- This function will returns entire file as a string, also display no of character available in files at last.
use_include_path
- First you need to set optional path if you want to use this argument.
set_include_path('/home/uttam/Desktop/');
- Now in readfile(‘index.txt’,true); if index.txt file not found then it will try to found this file in /home/uttam/Desktop
- Example
- echo readfile(‘index.txt’);
- file_get_contents ( string
$filename
, bool$use_include_path
= false , resource$context
, int$offset
= 0 , int$maxlen
)
- This function will fetch selected range portion of file as a string.
- This is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
readfile() VS file_get_contents()
readfile will read the file directly into the output buffer, and file_get_contents will load the file into memory, when you echo the result the data is copied from memory to the output buffer effectively using 2 times the memory of readfile.