[PHP] Loading a File into Variable - How??
I thought this would be fairly easy, but, I can't figure out how to load the contents of a file into a variable so I can output it later. The file to be loaded is in my include_path on the server and does contain some HTML. file_get_contents() is exactly what I need, but, it only works on a CVS version of PHP, whatever that is. I also tried the following function: function file_get_contents($filename) { $fd = fopen ($filename, r, 1); $contents = fread($fd, filesize($filename)); fclose($fd); return $contents; } But it returns nothing. If I use readfile() the file contents is displayed, but, what I really want to do is store it in a string variable, not output it directly. How can I do this? Thanks, Monty -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
ðÒÉ×ÅÔ! Monty wrote: But it returns nothing. If I use readfile() the file contents is displayed, but, what I really want to do is store it in a string variable, not output it directly. How can I do this? Look 4 implode() in the function list ÐÏËÁ áÌØÂÅÒÔÏ ëÉÅ× -_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_- LoRd, CaN yOu HeAr Me, LiKe I'm HeArInG yOu? lOrD i'M sHiNiNg... YoU kNoW I AlMoSt LoSt My MiNd, BuT nOw I'm HoMe AnD fReE tHe TeSt, YeS iT iS ThE tEsT, yEs It Is tHe TeSt, YeS iT iS ThE tEsT, yEs It Is... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
Look 4 implode() in the function list Implode isn't really what I need, 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
On Thu, Jul 18, 2002 at 04:28:57PM -0400, Monty wrote: Look 4 implode() in the function list Implode isn't really what I need, I just want to load an entire file into a single string variable. Yes, it IS what you need. Plus, to work around your other problems mentioned in later posts... $string = implode('', file('filename') ); --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
In 4.3 you would use file_get_contents() In prior versions I would suggest: $fp = fopen('filename','r'); $string = fread($fp, filesize('filename')); fclose($fp); The implode(file()) stuff is very memory-inefficient. -Rasmus On Thu, 18 Jul 2002, Analysis Solutions wrote: On Thu, Jul 18, 2002 at 04:28:57PM -0400, Monty wrote: Look 4 implode() in the function list Implode isn't really what I need, I just want to load an entire file into a single string variable. Yes, it IS what you need. Plus, to work around your other problems mentioned in later posts... $string = implode('', file('filename') ); --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
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: [EMAIL PROTECTED] (Andre Dubuc) Reply-To: [EMAIL PROTECTED] Newsgroups: php.general Date: Thu, 18 Jul 2002 17:45:14 -0400 To: Monty [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loading a File into Variable - How??
Hi Rasmus, file_gets_contents() doesn't work in my version of PHP (4.2.1). It says not a valid function or something like that. Also, I discovered that the filesize() function won't work on files fopened from the include_path. It returns a value of zero, so, I had to hardcode the bytes into the fread(). From: [EMAIL PROTECTED] (Rasmus Lerdorf) Newsgroups: php.general Date: Thu, 18 Jul 2002 15:14:13 -0700 (PDT) To: Analysis Solutions [EMAIL PROTECTED] Cc: PHP List [EMAIL PROTECTED] Subject: Re: [PHP] Loading a File into Variable - How?? In 4.3 you would use file_get_contents() In prior versions I would suggest: $fp = fopen('filename','r'); $string = fread($fp, filesize('filename')); fclose($fp); The implode(file()) stuff is very memory-inefficient. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php