Why Working with Files in PHP is a Must-Have for Any Developer
\n\nImagine: you are writing an online store. A client uploads an avatar, an administrator exports a CSV with products, and the system logs every action. Without the ability to work with the file system in PHP, these tasks will become a headache. But if you master the basic techniques, you will be able to manage data on the server as easily as editing notes on your phone.
\n\nIn this article, we will break down working with files in PHP from simple to complex. You will learn how to read and write data, check access rights, upload files to the server, and avoid common mistakes. Let's go!
\n\nBasics of Working with the PHP File System
\n\nBefore diving into the code, let's understand the main functions that PHP provides. They can be divided into three groups:
\n\n- \n
- Checking and metadata — whether a file exists, its size, type, and access rights. \n
- Reading and writing — open a file, read a line, write data, close it. \n
- Managing the file structure — creating, renaming, deleting files and folders. \n
Most operations in PHP for working with files require only one or two lines of code. But it is important to understand the context: where the file is located relative to your script, what rights the web server has, and how to handle errors.
\n\nChecking Existence and Access Rights
\n\nThe first thing to do before any operation is to make sure the file exists. For this, we use file_exists():
<?php\n$filename = 'config.php';\n\nif (file_exists($filename)) {\n echo "File $filename found!";\n} else {\n echo "File does not exist";\n}\n?>\n\nBut existence alone is not enough. You need to check if it can be read or written. For this, there are is_readable() and is_writable():
<?php\n$file = 'data.txt';\n\nif (is_readable($file)) {\n echo "Can read";\n}\nif (is_writable($file)) {\n echo "Can write";\n}\n?>\n\nThis is especially important when working with files in PHP in production — access rights (chmod) are often the cause of "Permission denied" errors.
\n\nReading Files in PHP: Three Ways for Every Taste
\n\nReading is the most common operation. PHP offers several approaches, each good in its own situation.
\n\nMethod 1: file_get_contents — for Small Files
\n\nIf you need to read the entire file into a string, this is the best option. Simple, fast, without unnecessary steps:
\n\n<?php\n$content = file_get_contents('readme.txt');\necho $content;\n?>\n\nGreat for configs, templates, small logs. But if the file weighs 200 MB, it's better not to — the server's memory will thank you.
\n\nMethod 2: fopen + fgets — for Large Files
\n\nWhen you need to process a huge CSV file or a gigabyte-sized log file, use line-by-line reading. This does not load the entire file into memory:
\n\n<?php\n$handle = fopen('bigfile.log', 'r');\nif ($handle) {\n while (($line = fgets($handle)) !== false) {\n // Process each line individually\n echo $line . "<br>";\n }\n fclose($handle);\n} else {\n echo "Error opening file";\n}\n?>\n\nNote: we must close the file using fclose(). This frees system resources.
Method 3: file() — Read into an Array of Lines
\n\nIf each line is a separate record (for example, a list of email addresses), you can immediately get an array:
\n\n<?php\n$lines = file('users.txt');\nforeach ($lines as $line_num => $line) {\n echo "Line #{$line_num}: " . htmlspecialchars($line) . "<br>";\n}\n?>\n\nThis method is convenient, but like file_get_contents, it loads