View Full Version : Assigning file contents to a variable
tbronson
12-08-2009, 03:47 PM
How can I assign the contents of a file to a PHP variable, so that the variable would insert the contents exactly as using INCLUDE would.
This is what I'm after:
REPLACE
...HTML...
<?php include('filepath/filename'); ?>
...
WITH
...HTML...
<?php echo $contents; ?>
...
where $contents contains the contents of filename.
Thanks!
bluewalrus
12-08-2009, 06:47 PM
$file = file_get_contents('filepath/filename.extension');
http://php.net/manual/en/function.file-get-contents.php
djr33
12-09-2009, 12:15 AM
Yes, file_get_contents().
However, this will not work "exactly" like include() because include also runs the PHP. file_get_contents() will just ECHO the text of the file (including PHP) like "<?php .....", not as actual PHP code.
If you do require the PHP to execute but its output be delayed, then you can use an output buffer with include.
Start the buffer, do the include, stop the buffer, then output the contents later when you'd like.
tbronson
12-09-2009, 01:22 AM
Yes, file_get_contents().
However, this will not work "exactly" like include() because include also runs the PHP. file_get_contents() will just ECHO the text of the file (including PHP) like "<?php .....", not as actual PHP code.
If you do require the PHP to execute but its output be delayed, then you can use an output buffer with include.
Start the buffer, do the include, stop the buffer, then output the contents later when you'd like.
Thanks. I forgot to mention that the included files contain HTML text and PHP snippets, so the not executing was what I was having problems with in the ways I tried.
So does that mean using ob_get_contents()?
http://php.net/manual/en/function.ob-start.php
It seems like things are getting complicated. I'm trying to create a very simple templating set-up, where files that I was including (INCLUDE) are instead assigned to variables, so I can insert different content at the same position, and keep conditional code out of the display page (like, using IF-THEN and a series of INCLUDES). For example:
FROM
...HTML...
<?php include('filepath/filename') ?>
...
TO
...HTML...
<?php echo $contents ?>
...
AND
$contents = filename or filename2 or ... depending on what's calling the page
The files would be HTML text and with PHP embedded, like, different headers, footers, sidebars.
Is there a simpler way to do this?
(This is a beginner's PHP forum, right? :)
Thanks.
djr33
12-09-2009, 01:27 AM
This is a PHP forum for any level, so answers will be what works best for your project.
Here this is getting a little advanced, but that's because there is no easy way to do it in PHP.
You are on the right track, and there should be plenty of examples of an output buffer at php.net. It literally stops output until you close the buffer then lets you get it into a variable and output it later. Simple (or complex) as that.
My advice, though I don't know your complete situation, is to rewrite your code and change your approach so that you just use an include.
Instead of processing and storing to a variable, just store the page name that you want to include. Then later instead of echo $contents; just do include($content); where $content is the path to the file.
Whenever you have layered includes (this happens in templating) things get messy, but a well written page rarely needs an output buffer. There are times it can help, but for the most part is just makes things more complex.
If you give some more information about the situation, we may be able to suggest other methods around it.
There is one more cheap trick you could try (though I don't really recommend it).
Because includes process PHP, this means they work with the variables in your page already (if $x is 5 in the main page, then any use of $x on the included page will be 5 as well, unless it is set differently at some point). But this is only true for locally included pages (ie, path = /some/thing.php). For pages on another server, it will process the PHP then grab the results and NOT make the pages interact. This means that you can use file_get_contents() for a page on a different server and it will have processed the PHP. This won't work if you do need the pages to interact (locally), though.
The way to fake having two servers is actually quite simple, but there are two things that this relies on:
1. This is the easy part: instead of using a relative path, use the actual URL, like file_get_contents('http://yoursite.com/page.php') instead of include('page.php').
2. This is the hard part: including remote files may be disabled on your server. Depending on the situation it may not be possible to enable it (if your host does not allow it, for example), and this can be hard to get working. It is especially bad if you plan to distribute the code because it will behave strangely on different servers. However, if you CAN use remote files, then there should be no problem here.
But just like using output buffers, there should be a better, more reliable method you can use if you approach the problem differently.
Hope this gets you started.
tbronson
12-09-2009, 05:35 AM
My advice, though I don't know your complete situation, is to rewrite your code and change your approach so that you just use an include.
Instead of processing and storing to a variable, just store the page name that you want to include. Then later instead of echo $contents; just do include($content); where $content is the path to the file.
Duh! Thanks, that's exactly what I was looking for, and so obvious, especially since I was already passing other small things like that by setting other variables already.
The other info was useful too, just to know about, and see how complicated it could've gotten...
Help with a few more really kinda braindead questions like this, and then I think I'll be thinking more in the PHP zone. :)
Thanks again!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.