How to read a file in PHP



This simple script can be used to read a file and displaying it contents into a web browser.

<?php 

//A function to read file content
function phpReadFile($filename){
	$myfile = fopen($filename, "r") or die("Unable to open file!");
	$filecontent = fread($myfile, filesize($filename));
	fclose($myfile);
	return $filecontent;
}

//Let's test it, try to read "MyFile.txt" file in root directory

echo nl2br(phpReadFile("MyFile.txt"));

?>

Watch the video here:

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *