Page 1 of 1
fopen or include which is more efficient
Posted: Mon May 15, 2006 12:01 pm
by budeboy
Hi
Im including the contents of a file on the webserver in a php page on the same server and trying to find out if using
Code: Select all
$fp = fopen("$file", "r");
$contents = fread($fp, filesize($file));
echo "$contents";
fclose($fp);
is more efficient than
Re: fopen or include which is more efficient
Posted: Mon May 15, 2006 12:46 pm
by santosj
For security reasons, you should use fopen if you are not going to execute any PHP.
To answer your question, if you aren't going to execute any PHP using fopen would be more efficient since PHP isn't going to try to parse the text.
Posted: Mon May 15, 2006 12:52 pm
by sava
I think the best way to use file_get_contents()...
Posted: Mon May 15, 2006 1:02 pm
by santosj
sava wrote:I think the best way to use file_get_contents()...
Is the same as calling what the person had above. Which works great if you only wanted to echo the contents, but it isn't very efficient if the person wants to do other stuff later. Perhaps the person wants to write some data back.
Code: Select all
file_put_contents($file, $string);
If used in the same script, the script would have opened and closed the same file twice when one fopen and fclose would be better use of the file handle.
Posted: Mon May 15, 2006 1:26 pm
by budeboy
thanks for the input its helped alot
As the file im going to be including is not going to be exicuted im gonna user the fopen method as it appears its more secure and efficient
Thanks
