Web Hosting Talk







View Full Version : make a files content a variable


UrlGuy
07-13-2005, 09:14 PM
Hey,

How do I make a files content a variable?
Like if a file named output.txt contains the following line:
sometext blablabla 0123456789 more test
how would I use PHP to put that into a variable?

I know maybe fread or so but I am not sure how to use it!:\

Can anyone please show me some code on how to do this or give me some start help?
All tips apprecciated, thanks!!

WLHosting
07-13-2005, 09:26 PM
Try using:

<?
$wp=fopen("output.txt","r");
// here you receive content of page
while(!feof($wp)) {
$content .= fread($wp,4096); // load the contents into a variable
}
fclose($wp); // close output.txt
echo $content; // display the contents of output.txt which is stored in $content
?>

VolkNet
07-13-2005, 09:38 PM
Even easier way to do it, if you're running the php 4.3.x or newer


$contents = file_get_contents("output.txt");


The php site recommends doing it this way because it's utilizes the OS (if supported) and makes reading the file faster :)

UrlGuy
07-13-2005, 09:40 PM
Thanks alot!

WLHosting
07-13-2005, 10:10 PM
Nice, I have not come across that function before. I guess I can say thanks as well.

I guess I need to go hit up php.net even more to check out all the functions. (Better take a couple days :))

VolkNet
07-13-2005, 10:25 PM
hehe no problem.