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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

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

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

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

Post by calvinmicklefinger »

Thanks.
Post Reply