Page 1 of 1

How do I read .txt file and use contents as string variable

Posted: Sat Jan 19, 2008 5:29 pm
by calvinmicklefinger
Complete and utter noob here, trying to use the contents of input.txt as a string variable

Only snippet I've found that seems to resemble what I need ....

<?php
$filename="input.txt";
$output="";
$file = fopen($filename, "r");
while(!feof($file)) {
$output = $output . fgets($file, 4096);
}
fclose ($file);
echo $output;
?>

So, can I merely use the $output variable to insert a string into my script?

Or is there a better method?

Many Thanks,
Kirk

Re: How do I read .txt file and use contents as string variable

Posted: Sat Jan 19, 2008 5:31 pm
by John Cartwright
file() if you want to read all the lines into an array, or
file_get_contents() if you want to read the file into a string.

Re: How do I read .txt file and use contents as string variable

Posted: Sat Jan 19, 2008 6:57 pm
by calvinmicklefinger
Thanks.