Working with the File System in PHP: Practical Examples and Tips

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with the File System in PHP: From Basics to Real Projects

\n\n

Working with the file system is one of the key tasks for any web developer. PHP provides a powerful set of built-in functions for creating, reading, editing, and deleting files, as well as managing directories. In this article, we will explore practical examples that will help you master working with files in PHP and apply this knowledge in real projects.

\n\n

Whether you are creating a logging system, a file uploader, or simply saving user data, understanding the basics of the file system in PHP is an essential skill. Let's move on to practice.

\n\n

Basic Functions for Working with Files

\n\n

PHP provides several ways to interact with files. The most popular ones are using fopen, fwrite, fread, and the high-level functions file_get_contents and file_put_contents. Let's look at them with examples.

\n\n

Creating and Writing to a File

\n\n

To create a file and write to it, use the fopen function with the 'w' mode (write with truncation) or 'a' mode (append to the end).

\n\n
<?php\n$filename = 'log.txt';\n$file = fopen($filename, 'w');\n\nif ($file) {\n    fwrite($file, "Line 1: Writing to file\\n");\n    fwrite($file, "Line 2: Another entry\\n");\n    fclose($file);\n    echo "File successfully created and written.";\n} else {\n    echo "Error opening file.";\n}\n?>
\n\n

If you need to quickly write data without managing a pointer, use file_put_contents:

\n\n
<?php\n$data = "New content for the file.\\nSecond line.";\nfile_put_contents('data.txt', $data);\necho "Data written.";\n?>
\n\n

Reading a File

\n\n

To read a file line by line, use fgets in a loop:

\n\n
<?php\n$file = fopen('log.txt', 'r');\nif ($file) {\n    while (($line = fgets($file)) !== false) {\n        echo $line . "<br>";\n    }\n    fclose($file);\n}\n?>
\n\n

An alternative is file_get_contents for reading the entire content into a string:

\n\n
<?php\n$content = file_get_contents('log.txt');\necho nl2br($content);\n?>
\n\n

Checking File Existence and Properties

\n\n

Before working with a file, it is important to ensure it exists and is accessible for reading or writing. PHP provides the functions file_exists, is_readable, is_writable, and filesize.

\n\n

Example of Checking a File

\n\n
<?php\n$file = 'config.php';\n\nif (file_exists($file)) {\n    echo "File exists.<br>";\n    \n    if (is_readable($file)) {\n        echo "File is readable.<br>";\n        echo "File size: " . filesize($file) . " bytes.";\n    } else {\n        echo "File exists but is not readable.";\n    }\n} else {\n    echo "File not found.";\n}\n?>
\n\n

Checking the File Type

\n\n

The filetype function returns the file type: 'file', 'dir', 'link', and others.

\n\n
<?php\n$path = 'uploads';\nif (file_exists($path)) {\n    echo "Type: " . filetype($path); // will output 'dir' if it is a folder\n}\n?>
\n\n

Managing Directories

\n\n

Working with folders in PHP is no less important. You can create, delete, and scan directories.

\n\n

Creating and Deleting Folders

\n\n
<?php\n// Creating a folder\nif (!file_exists('backup')) {\n    mkdir('backup', 0777, true); // third parameter true — recursive creation\n    echo "Folder created.";\n}\n\n// Deleting an empty folder\nif (file_exists('temp')) {\n    rmdir('temp');\n    echo "Folder deleted.";\n}\n?>
\n\n

Scanning Directory Contents

\n\n

The scandir function returns an array of all files and folders

Blogs

Book Recommendations