fgets() and fread() - What is the difference?
Asked Answered
R

5

40

I understand the differences between fgets() and fgetss() but I don't get the difference between fgets() and fread(), can someone please clarify this subject? Which one is faster? Thanks!

Rickard answered 1/5, 2010 at 21:36 Comment(1)
For C, this appears to be the oldest duplicate of this question: Difference between fgets and fread. But, I think the general concepts in the PHP answers apply equally as well.Romanesque
J
71

fgets reads a line -- i.e. it will stop at a newline.

fread reads raw data -- it will stop after a specified (or default) number of bytes, independently of any newline that might or might not be present.


Speed is not a reason to use one over the other, as those two functions just don't do the same thing :

  • If you want to read a line, from a text file, then use fgets
  • If you want to read some data (not necessarily a line) from a file, then use fread.
Jenisejenkel answered 1/5, 2010 at 21:38 Comment(0)
L
1

fread() for binary data and fread has a limit on how many chars you can read

$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
    $buffer = fread($source_file, 5);  
   var_dump($buffer); //return string with length 5 chars!
}

Number 5 is length bytes have been read .

Louisville answered 28/10, 2015 at 4:21 Comment(0)
G
0

The function fgets reads a single line from a text file. It is reading so long until the end of the current line (or the end of the file) is reached. Therefore, if you would like to read one line from a text file, you should use fgets. The function fread not only reads until the end of the line but to the end of the file [e.g. fread($handle)] or as many bytes as specified as a parameter [e.g. fread($handle, 1024)]. So, if you would like to read a complete file, no matter whether it is a text file with all containing lines or arbitrary raw data from a file, you should use fread.

Gibber answered 25/9, 2016 at 15:47 Comment(0)
C
0

Both the functions are used to read data from files

fgets($filename, $bytes) fgets usually reads $bytes-1 amount of data and stops at a newline or an EOF(end-of-file) whichever comes first. If the bytes are not specified, then the default value is 1024 bytes.

fread($filename, $bytes) fread reads exactly $bytes amount of data and stops only at EOF.

Crowd answered 16/12, 2019 at 3:10 Comment(0)
K
0

The accepted answer is correct, but there is one more case for fread to stop reading. fread has a chunk limit of 8192 bytes. I discovered this when I was getting different results from fread($stream, 8300) and fget($stream, 8300).

From fread docs:

if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

Kulak answered 1/9, 2020 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.