Results 1 to 6 of 6

Thread: Assigning file contents to a variable

  1. #1
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Assigning file contents to a variable

    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

    Code:
    ...HTML...
    <?php include('filepath/filename'); ?>
    ...
    WITH

    Code:
    ...HTML...
    <?php echo $contents; ?>
    ...
    where $contents contains the contents of filename.


    Thanks!

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Code:
    $file = file_get_contents('filepath/filename.extension');
    http://php.net/manual/en/function.file-get-contents.php

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    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

    Code:
    ...HTML...
    <?php include('filepath/filename') ?>
    ...
    TO

    Code:
    ...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.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Last edited by djr33; 12-09-2009 at 01:33 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •