Jump to content

Faster?


unidox

Recommended Posts

well if you look at the manual for both file_get_contents and fread, they both say that file_get_contents is preferred because it gives much better performance.  But that's if you're just wanting to get all the contents of the file.  fread is more versatile.  Depends on what it is you're trying to do.

Link to comment
https://forums.phpfreaks.com/topic/110420-faster/#findComment-566505
Share on other sites

also, for larger files, I would imagine fread would save you some memory (as long as you process each chunk as it arrives and not just append it to a variable)

nope. from the manual:

Note: If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance than the code above.

("the code above" is an example of how to use fread to get the contents of a file into a string).

Link to comment
https://forums.phpfreaks.com/topic/110420-faster/#findComment-566528
Share on other sites

right...cus it just appends it to a variable...put if you process it in chunks:

 

<?php
  $handle = fopen("path/to/some/large_file.ext", "r");
  $contents = '';
  while (!feof($handle)) {
    echo fread($handle, 1024);
  }
  fclose($handle);
?>

 

in this case, you aren't eating up memory by storing the entire contents into a variable

Link to comment
https://forums.phpfreaks.com/topic/110420-faster/#findComment-566534
Share on other sites

all you're going to do is read 1 digit? And you're concerned about performance? LoL...dude even with huge files like megabytes huge, we're talking microseconds difference in time here.. I can't even imagine how small of a difference we're talking about when reading 1 little character. 

Link to comment
https://forums.phpfreaks.com/topic/110420-faster/#findComment-566558
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.