Cheat Sheets & Quick References

PHP File Handling Cheat Sheet

A quick reference for reading, writing, checking and safely handling files in PHP applications.

Overview

PHP provides filesystem functions for reading, writing, copying, moving and inspecting files. File operations should be designed with permissions, path validation and upload risks in mind.

Common functions

  • file_get_contents() reads a file into a string.
  • file_put_contents() writes data to a file.
  • is_file() and is_dir() check filesystem object types.
  • is_readable() and is_writable() check access permissions.
  • mkdir() creates directories; rename() moves or renames paths.
  • fopen() and related stream functions support controlled file access.

Security notes

Do not concatenate untrusted input into filesystem paths. Resolve and constrain paths to an intended storage directory, generate server-side filenames for uploads and store private files outside the public web root when possible.

Official resource

PHP Filesystem Functions