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
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
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
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
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.