Working with the File System in PHP: From Basics to Real Projects
\n\nWorking 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\nWhether 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\nBasic Functions for Working with Files
\n\nPHP 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.
Creating and Writing to a File
\n\nTo 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).
<?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\nIf you need to quickly write data without managing a pointer, use file_put_contents:
<?php\n$data = "New content for the file.\\nSecond line.";\nfile_put_contents('data.txt', $data);\necho "Data written.";\n?>\n\nReading a File
\n\nTo read a file line by line, use fgets in a loop:
<?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\nAn alternative is file_get_contents for reading the entire content into a string:
<?php\n$content = file_get_contents('log.txt');\necho nl2br($content);\n?>\n\nChecking File Existence and Properties
\n\nBefore 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.
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\nChecking the File Type
\n\nThe filetype function returns the file type: 'file', 'dir', 'link', and others.
<?php\n$path = 'uploads';\nif (file_exists($path)) {\n echo "Type: " . filetype($path); // will output 'dir' if it is a folder\n}\n?>\n\nManaging Directories
\n\nWorking with folders in PHP is no less important. You can create, delete, and scan directories.
\n\nCreating 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\nScanning Directory Contents
\n\nThe scandir function returns an array of all files and folders