fopen or include which is more efficient

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
budeboy
Forum Newbie
Posts: 2
Joined: Mon May 15, 2006 11:58 am

fopen or include which is more efficient

Post 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

Code: Select all

include($file);
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Re: fopen or include which is more efficient

Post 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.
Last edited by santosj on Mon May 15, 2006 1:04 pm, edited 1 time in total.
sava
Forum Newbie
Posts: 8
Joined: Mon May 15, 2006 10:04 am

Post by sava »

I think the best way to use file_get_contents()...
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

sava wrote:I think the best way to use file_get_contents()...

Code: Select all

echo file_get_contents($file);
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.
budeboy
Forum Newbie
Posts: 2
Joined: Mon May 15, 2006 11:58 am

Post 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 :)
Post Reply