Showing posts with label fread(). Show all posts
Showing posts with label fread(). Show all posts

Monday 30 July 2012

PHP fread Function


PHP - File Read

If you wanted to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.

PHP Code:

$myFile = "testfile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

Output:

Floppy Jalopy Pointy Pinto 
Note: It is all on one line because our "testfile.txt" file did not have a <br /> tag to create an HTML line break. Now the entire contents of the testfile.txt file is stored in the string variable $theData.