Reading from a file can be carried out in 2 ways one is a „low-level” reading data from a file using fread (int index, int length). It reads the data read from the file specified by the previously created „indicator”. The data are read to the length specified in the second parameter. If the data is less than the specified length, it will be returned all the data from the file.
Fread () moves the internal file pointer – to the place where the reading ended.
$fp = fopen („test.txt”, „r”);
$text =fread ($fp, 10);
function fopen opened a file for reading and then read it was 10 characters, so the variable $text will include „This is a pr.” Has been moved inside the file pointer, so the next call to the line „$text = fread ($fp,10)” will, that this variable will be the text „exemplary” – then reading started from the point at which ended read previous call to this function .
How to read the entire file into a variable?
You have to use one more function – filesize (string filename). It differs from other functions that do not charge as a parameter a pointer to a file but the file name. Download the whole file to a variable, you can get one ruler.
$data = fread(fopen („filename”, „r”), filesize („filename”));
Fread () will read the data without interruption – from the beginning to the end of the file, ignoring the signs of the end of the line – for this function is a simple sign.
The entire file can be read also in another way. The file ($filename) returns an array in which each element is a separate line of the file whose name is given in the parameter. This function can be used to load the entire file into a single string.