Working with the File System in PHP: Basics and Practical Examples
Working with the file system is one of the key tasks in PHP web development. Whether it's uploading images, writing logs, reading configuration files, or managing temporary data — knowledge of functions for working with files and directories is essential for every PHP developer. In this article, we will thoroughly examine the main operations: opening, reading, writing, closing files, as well as managing folders and access permissions.
Basic Functions for Working with Files in PHP
PHP provides a rich set of built-in functions. Let's look at the most important ones.
Opening and Closing a File: fopen() and fclose()
To start working with a file, you need to open it using fopen(). The function takes two required parameters: the path to the file and the open mode. The mode determines what you can do with the file: read, write, append data to the end, etc.
Main modes:
r— read-only. Pointer at the beginning of the file.r+— reading and writing. Pointer at the beginning.w— write-only. If the file exists, it is cleared. If not, it is created.w+— reading and writing. Clears the file or creates a new one.a— write-only to the end of the file. If the file does not exist, it is created.a+— reading and writing to the end.
<?php$file = fopen("example.txt", "w"); // Open file for writingif ($file) { echo "File opened successfully."; fclose($file); // Always close the file after working} else { echo "Failed to open file.";}?>Reading from a File: fgets(), fread() and file_get_contents()
The simplest way to read an entire file is the file_get_contents() function. It returns the content as a string.
<?php$content = file_get_contents("example.txt");echo $content;?>If you need to read line by line (for example, for large files), use fgets() in a loop:
<?php$file = fopen("example.txt", "r");while (!feof($file)) { $line = fgets($file); echo $line . "<br>";}fclose($file);?>The fread() function allows you to read a specified number of bytes:
<?php$file = fopen("example.txt", "r");$content = fread($file, 100); // Read the first 100 bytesecho $content;fclose($file);?>Writing to a File: fwrite() and file_put_contents()
For writing data to a file, it is convenient to use file_put_contents(). It opens the file, writes the string, and closes it in one command.
<?php$data = "Hello, world!";file_put_contents("example.txt", $data);?>If you need to append data to the end, use the FILE_APPEND flag:
<?php$data = "New line";file_put_contents("example.txt", $data, FILE_APPEND);?>For more flexible writing (for example, in binary mode), fwrite() is used:
<?php$file = fopen("example.txt", "w");fwrite($file, "String to write");fclose($file);?>Working with Directories in PHP
In addition to files, PHP allows you to manage folders: create, delete, and read their contents.
Creating and Deleting Folders: mkdir() and rmdir()
The mkdir() function creates a new directory. The second parameter can specify access permissions (default is 0777).
<?phpif (mkdir("new_folder", 0777)) { echo "Folder created.";} else { echo "Error creating folder.";}?>To delete an empty folder, use