Before we can read information from a file we have to use the function fopen to open the file for reading.
The fread function is the staple for getting data out of a file. The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read. One character is equal to one byte.
If you want to read the first five characters then you would use five as the integer.
$myFile = “testfile.txt”;
$fh = fopen($myFile, ‘r’);
$theData = fread($fh, 5);
If you want to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.
$theData = fread($fh, filesize($myFile));