Working with Files in PHP: From Beginner to Pro in 20 Minutes

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

Why Working with Files in PHP is a Must-Have for Any Developer

\n\n

Imagine: 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\n

In 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\n

Basics of Working with the PHP File System

\n\n

Before 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
\n\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\n

Checking Existence and Access Rights

\n\n

The first thing to do before any operation is to make sure the file exists. For this, we use file_exists():

\n\n
<?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\n

But 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():

\n\n
<?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\n

This is especially important when working with files in PHP in production — access rights (chmod) are often the cause of "Permission denied" errors.

\n\n

Reading Files in PHP: Three Ways for Every Taste

\n\n

Reading is the most common operation. PHP offers several approaches, each good in its own situation.

\n\n

Method 1: file_get_contents — for Small Files

\n\n

If 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\n

Great 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\n

Method 2: fopen + fgets — for Large Files

\n\n

When 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\n

Note: we must close the file using fclose(). This frees system resources.

\n\n

Method 3: file() — Read into an Array of Lines

\n\n

If 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\n

This method is convenient, but like file_get_contents, it loads

Blogs

Book Recommendations