Introduction: Why You Need to Know How to Work with the File System in PHP
Any web developer sooner or later faces the need to save data on the server, upload images, create temporary files, or read configurations. PHP provides a powerful set of functions for working with the file system that allow you to perform all these tasks quickly and securely.
In this article, we will cover the most in-demand operations: reading and writing files, creating and deleting directories, checking the existence of items, and working with paths. All examples are written in PHP and are ready to use in your projects.
Basic Functions for Working with Files
Let's start with basic operations — opening, reading, and writing files. PHP supports several ways to interact with files, and we will look at the most popular ones.
Opening and Closing a File: fopen and fclose
The fopen() function opens a file and returns a handle for further work. The opening modes determine what you can do with the file: read (r), write (w), append (a), etc.
<?php// Open a file for reading$file = fopen("data.txt", "r");if ($file) { // Work with the file fclose($file); // Always close it} else { echo "Failed to open file";}?>Reading from a File: fread, fgets, file_get_contents
To read the contents, you can use different functions depending on the task:
fread()— reads a specified number of bytes.fgets()— reads one line.file_get_contents()— the simplest way to read an entire file at once.
<?php// Reading the entire file$content = file_get_contents("example.txt");echo $content;
// Reading line by line$file = fopen("example.txt", "r");while (($line = fgets($file)) !== false) { echo $line . "<br>";}fclose($file);?>Writing to a File: fwrite and file_put_contents
Writing data is done using fwrite() (requires an open handle) or file_put_contents() (one line — and the file is written).
<?php// Quick writefile_put_contents("log.txt", "New entry", FILE_APPEND);
// Writing via fwrite$file = fopen("config.ini", "w");fwrite($file, "db_host=localhost");fwrite($file, "db_user=root");fclose($file);?>Working with Directories
Creating, deleting, and navigating folders is another important part of working with the file system in PHP.
Creating and Deleting Folders: mkdir and rmdir
The mkdir() function creates a new directory. The third parameter recursive allows you to create nested folders with a single command.
<?php// Create a single foldermkdir("uploads", 0777);
// Create nested folders (2025 → 03 → images)mkdir("uploads/2025/03/images", 0777, true);
// Delete an empty folderrmdir("uploads/temp");?>Checking the Existence of Files and Folders
Before performing operations, it is always useful to check whether a file or directory exists. The functions file_exists(), is_file(), and is_dir() are used for this.
<?php$path = "data/config.php";if (file_exists($path)) { echo "File or folder exists";}if (is_file($path)) { echo "This is a file";}if (is_dir("uploads")) { echo "This is a directory";}?>Getting a List of Files in a Directory
The scandir() function returns an array of all items in a folder. And glob() allows you to filter by a pattern (for example, only images).
<?php// All files and folders$items = scandir("uploads");print_r($items);
// Only PHP files$phpFiles = glob("*.php");for