-1

I'm trying to read a local text file in PHP, and I have the line of code so that it does read the file, however it echos it when I don't want it to?
$teacher1department = readfile("dir/test.txt"); I want that to save as a variable so I can use it later, however it instantly prints it to the screen? How would I fix this error?

  • Warning: fopen() expects at least 2 parameters, 1 given in /home/www.exmaple.com/dir/index.php on line 16 – Hugh Chamers Dec 22 '16 at 19:27
  • 1
    Did you try reading the PHP documentation of `readfile()`? It says: **Reads a file and writes it to the output buffer.** – Barmar Dec 22 '16 at 19:30
  • If you then go down to the **See Also** section you'll see a link to the function you want. – Barmar Dec 22 '16 at 19:31
  • **`file_get_contents()` - Reads entire file into a string** – Barmar Dec 22 '16 at 19:31
  • 1
    Possible duplicate of [Read a plain text file with php](http://stackoverflow.com/questions/4103287/read-a-plain-text-file-with-php) – Meneer Venus Dec 22 '16 at 19:42

1 Answers1

1
$teacher1department = file_get_contents("dir/test.txt");
Barmar
  • 741,623
  • 53
  • 500
  • 612
Ying Hong
  • 36
  • 2