Re: Loading a File into Variable - How??
| From: | Monty | Date: | Fri, 19 Jul 2002 04:18:58 +0000 |
| Subject: | Re: Loading a File into Variable - How?? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-108424@lists.php.net to get a copy of this message | ||
Andre, here's the function that worked for me...
function file_get_contents($filename) {
$fd = fopen($filename, "r", 1);
$contents = fread($fd, 12000);
fclose($fd);
return $contents;
}
$page_string = file_get_contents("my_file.php");
The third parameter in fopen() [1] can be removed if you don't want to look
for files in your include_path. The second parameter in fread() [12000] is
where I hardcoded the filesize. Increase that number if you'll be opening
larger files.
Originally I had "12000" replaced with "filesize($filename)" but if the
$filename was opened from the include_path, this seems to always return
zero, which is why I hardcoded the byte size into fread().
Monty
> From: aajdubuc@webhart.net (Andre Dubuc)
> Reply-To: aajdubuc@webhart.net
> Newsgroups: php.general
> Date: Thu, 18 Jul 2002 17:45:14 -0400
> To: Monty <monty3@hotmail.com>
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Loading a File into Variable - How??
>
> Hi Monty,
>
> I've been trying to do the same thing with no success. Would you be so kind
> as to show me how you finally did it? I'm not too clear what you meant by:
>
> "So, if I replace the filesize($filename) command with a hard-coded number,
> it works."
>
> Tia,
> Andre
>
> On Thursday 18 July 2002 04:28 pm, you wrote:
>> I just want to load an entire file into a
>> single string variable.
>>
>> However, I figured out the problem shortly after posting that first message
>> (of course). Because the file being opened is in the include_path, it seems
>> filesize() doesn't see those files. So, if I replace the
>> filesize($filename) command with a hard-coded number, it works.
>>
>> Monty