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.


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

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

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


Watch the video here:


Leave a Reply

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