CodyBugstein
CodyBugstein

Reputation: 23322

PHP fgets() won't work unless it's implemented with a variable?

I'm writing some code that can read in from a .txt file a display it on a webpage.

I had problems in my initial code, in that it would read in any text and it would erase whatever was in the document.

My original code:

function readIn(){
        $input = fopen("input.txt", "r"); //Open the file, save opened file in input
        $line = fgets($input);
        fclose($input);
        return $line
}

It only started working once I put in a While loop to go through EVERY LINE

function readIn(){
        $input = fopen("input.txt", "r"); //Open the file, save opened file in input
        $fullText = ""; //Variable full text
        while(!feof($input)){
            $line = fgets($input);
            $fullText = $fullText . $line;
        }
        fclose($input);
        return $fullText;
}
echo readIn();

Upvotes: 0

Views: 663

Answers (1)

J.D. Pace
J.D. Pace

Reputation: 616

Use "file_get_contents" to read an entire file into a variable, and then output in whatever fashion you choose.

Upvotes: 1

Related Questions