[thelist] File Include Tip

Simon Willison simon at incutio.com
Mon May 6 18:44:01 CDT 2002


At 16:52 06/05/02 -0500, Burhan Khalid wrote:
>         Thought I'd share an alternate way to include a file (using PHP),
> that
>doesn't use SSI's.
>
><tip type="PHP File Include" author="Burhan Khalid">
>
>         Want to include a file in your page? Use this :
>
>         function getContent ($fileName, &$body) {
>                 $body .= fread (fopen($fileName,"r"), filesize ($fileName));
>         }
>
>         call it like this :
>
>         getContent("include.txt", $body);
>
>         now simply place <?= $body; ?> where you need the content to go.
></tip>

I may be misunderstanding your tip, but this doesn't seem necessary. PHP's
include() function (despite its name) has nothing to do with SSIs in the
traditional sense - it will work on any platform (it's not just limited to
Apache) and is virtually the functional equivalent of your code in the tip,
except include() can run PHP code in the file it is used on. That said, a
function to return the contents of a file in to a variable can be very
useful for other PHP work (although I would have it return the contents
rather than use a reference variable).

Cheers,

Simon Willison




More information about the thelist mailing list