unidox Posted June 16, 2008 Share Posted June 16, 2008 I was wondering which is faster(ie. less site load time). File_get_contents or using fopen/fread. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/ Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566505 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566521 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566528 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566534 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 well that's fine and dandy if all you're wanting to do is echo out the contents. Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566541 Share on other sites More sharing options...
unidox Posted June 16, 2008 Author Share Posted June 16, 2008 Well all I am going to read is 1 digit. A 1 or a 0. Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566548 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 well then use file_get_contents() Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566551 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566558 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 out of curiosity, why are you reading 1 digit from the file? Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566570 Share on other sites More sharing options...
DarkWater Posted June 16, 2008 Share Posted June 16, 2008 Yeah, what exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566581 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 my first guess is a page hit counter Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566718 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 mine was a 'lock' of some sort...in which i was going to recommend flock Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-566729 Share on other sites More sharing options...
unidox Posted June 28, 2008 Author Share Posted June 28, 2008 Well it is for a license script. It checks license.php on a remote server with a license key and server info using get, and then license.php outputs a 1 for yes, 0 for no. Quote Link to comment https://forums.phpfreaks.com/topic/110420-faster/#findComment-576558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.