File handling

File handling lets PHP interact with files on the server, like reading logs, exporting data, or serving downloads.

readfile() is the simplest file function:

  • It reads a file
  • It immediately outputs its contents
  • It returns the number of bytes read (or false on failure)

Important points:

  • readfile() already outputs the file, no echo needed
  • Paths should be explicit and safe
  • In WordPress, always anchor paths using __DIR__

This function is best when:

  • You only need to display or serve a file
  • You do not need to manipulate its contents