Using readfile() to read HTML to a variable

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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Using readfile() to read HTML to a variable

Post by jayshields »

Hi again dudes.

This question shouldn't be so hard :)

Anyway...

I have 2 HTML files on my server, and I want to take all of the contents of each file into a variable in a PHP script.

So, f.e.

Code: Select all

$message = readfile('fullurltohtmlfile');
$message .= $_POST['messagebody'];
$message .= readfile('fullurltohtmlfile');
Is this the best way to go about doing this? Is there a better function for this job?

I have tried the above, but get this error:

Code: Select all

PHP Warning: readfile(http://www.wrightandshields.co.uk/JayMail/templates/WrightandShields/header.html): failed to open stream: Bad file descriptor in D:\Webspace\wrightandshields.co.uk\wwwroot\JayMail\index.php on line 66 PHP Warning: readfile(http://www.wrightandshields.co.uk/JayMail/templates/WrightandShields/footer.html): failed to open stream: Bad file descriptor in D:\Webspace\wrightandshields.co.uk\wwwroot\JayMail\index.php on line 68 PHP Fatal error: Maximum execution time of 30 seconds exceeded in D:\Webspace\wrightandshields.co.uk\wwwroot\JayMail\index.php on line 68
Thanks for any help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

readfile() opens and sends the file directly to the browser, pretty much, it doesn't return the contents. If you want the contents in a single string, file_get_contents() is what you want. It seems these files are local to this script though, is this correct? If so, you don't need to use a full url to the files. This is because using a full url requires php to open a connection to the location to retrieve the file, even if it's on the server php is on.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

that worked great.

thanks alot.
Post Reply