To perform this operation, we will use the r
mode in fopen()
and the fread()
function to access the content.
In the first parameter of this function, we will write the variable of the file we opened with fopen. In the second parameter, we will specify how many bytes we will read from the file.
<?php
$file = fopen('hello.txt', 'r');
$content = fread($file, filesize('hello.txt'));
echo $content;
fclose($file);
Now, if there is something in our hello.txt
file, it will be written on the screen exactly as it is. If it is blank, nothing will be written on the screen.
Since we want to read the entire content of the file, we specify the total size of the file with the filesize()
function in the second parameter, so we get all the content.