PHP theorytheory 0/50 · 0%
Fundamentals · medium

33. File I/O

Reading and writing files.

PHP offers both simple helpers like file_get_contents() and file_put_contents(), and lower-level stream functions fopen()/fread()/fwrite()/fclose() for finer control over large files. File existence and permissions can be checked with file_exists() and is_writable().

When working with user-uploaded paths or file names, always validate and sanitize them to prevent path traversal attacks (e.g., '../../etc/passwd').

Check your understanding

  1. 1. Which function reads an entire file into a string in one call?

  2. 2. What risk arises from using unsanitized user input as a file path?