How to Read From a File in PHP

Configurare noua (How To)

Situatie

The fread function allows you to read from a file. It’s similar to fwrite, but you need to provide the length in bytes you want to read.

Solutie

<?php
$file_handle = fopen('/home/tutsplus/files/tmp.txt', 'r');
$contents = fread($file_handle, filesize('C:\Users\flcri\Desktop\Solutii\3\test.txt'));
fclose($file_handle);
?>

As we want to read from the C:\Users\flcri\Desktop\Solutii\3\test.txt file, we’ve opened it with the r mode. Next, we’ve used the fread function to read all the contents of the file into the $content variable.

The first argument of the fread function is the file system pointer, so that it knows where to read from. The second argument is the length in bytes you want to read from a file. In our case, we want to read all the contents of the C:\Users\flcri\Desktop\Solutii\3\test.txt file, and thus we’ve used the filesize function to measure the size of the file.

Tip solutie

Permanent

Voteaza

(3 din 10 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?