top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is the main difference between fread() and readfile() in PHP?

0 votes
1,335 views
What is the main difference between fread() and readfile() in PHP?
posted Aug 10, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Function readfile() => is a simple function used to read the data from a file without doing any further text processing like calculating the size of data to be print. It's just like read and then print.

Function fread() => also do the same thing but with the difference that you can specify the size of the data you want to read form the file. This can be helpful when you have larger files and you only want to read some chunks of data from it.

With fread() function you can specify the file handle too.

<?php
$myfile= "/usr/local/something.txt";
$handle = fopen($filename, "r");
    $contents = fread($handle, filesize($myfile));
?> 
answer Aug 13, 2014 by anonymous
Similar Questions
+3 votes

Discovered today: fopen() opens '.' and '..' but fread() returns nothing without errors nor end-of-file, so this loop:

 $f = fopen('.', 'r');
 if( $f === FALSE ) die("fopen failed");
 $n = 0;
 while(($s = fread($f, 10)) !== FALSE)
 $n += strlen($s);
 fclose($f);
 echo "total bytes read: $nn";

never ends! The same happens with '..'. Tested on PHP 5.6.3 and 7.0.0 under Slackware 14.1 32-bits. Under Vista, instead, fopen() fails as expected.

Bug or specific feature under Linux?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...