Current Location: Home> Latest Articles> PHP File Operations Guide: Methods for Reading and Writing Files

PHP File Operations Guide: Methods for Reading and Writing Files

gitbox 2025-06-18

1. Overview

In a complete web application, the file system is a crucial component. PHP provides numerous functions to read, write, and manage files. This article covers the essential methods for handling files in PHP.

2. Opening Files

In PHP, you can open files using the fopen() function. This function requires two parameters: the filename and the mode in which to open the file.

2.1 File Opening Modes

The second parameter of the fopen() function is the mode used to open the file. PHP supports the following opening modes:

  • r: Read-only mode, file pointer at the beginning of the file.
  • r+: Read-write mode, file pointer at the beginning of the file.
  • w: Write-only mode, clears file content before opening.
  • w+: Read-write mode, clears file content before opening.
  • a: Write-only mode, file pointer at the end of the file.
  • a+: Read-write mode, file pointer at the end of the file.
  • x: Write-only mode, creates a new file; fails if the file already exists.
  • x+: Read-write mode, creates a new file; fails if the file already exists.

Note: When using modes like w, w+, a, a+, x, or x+, be cautious to avoid accidentally deleting files or losing data. In most cases, read-only and read-write modes are sufficient.

2.2 Opening a File

Opening a file is simple with the fopen() function:

$file = fopen("file.txt", "r");

The above code opens the file "file.txt" in read-only mode.

3. Reading Files

Once a file is opened, you can read its contents using the fread() function. The first parameter is the file resource, and the second is the number of bytes to read. For example, the following code reads 10 bytes from the file:

$file = fopen("file.txt", "r");
echo fread($file, 10);
fclose($file);

The fread() function reads data starting from the current file pointer position. The file pointer moves forward by the number of bytes read.

4. Writing to Files

You can write data to a file using the fwrite() function. The first parameter is the opened file, and the second is the data to write.

$file = fopen("file.txt", "w");
fwrite($file, "Hello World!");
fclose($file);

The code above overwrites the content of "file.txt" with "Hello World!".

5. Closing Files

After reading or writing to a file, it is important to close it using the fclose() function. It takes one parameter: the file to close.

$file = fopen("file.txt", "r");
fclose($file);

6. File Pointers

A file pointer is a special marker indicating the current position in the file for PHP file operations. The fseek() function can be used to move the file pointer to a specific position. The first parameter is the opened file, the second is the position to move to, and the third is the reference point (file start, current position, or file end).

$file = fopen("file.txt", "r+");
fseek($file, 10);
echo fread($file, 10); // Reads 10 bytes from the file
fclose($file);

The code above moves the file pointer to position 10 and then reads 10 bytes of data from the file.

7. Deleting Files

You can delete a file using the unlink() function. It takes one parameter: the name of the file to delete.

$file = "file.txt";
unlink($file);

The code above deletes the file named "file.txt".

8. Relative and Absolute Paths

8.1 Relative Path

A relative path is the path relative to the directory where the current PHP script resides. For example:

// Script located at /var/www/html/test.php
$file = "file.txt"; // Relative path

The above code will look for "file.txt" in the "/var/www/html" directory.

8.2 Absolute Path

An absolute path is the full path in the file system. For example:

$file = "/home/user/file.txt"; // Absolute path

The above code will look for "file.txt" in the "/home/user/" directory.

Conclusion

PHP file operations are an essential part of web applications. This article covered how to open, read, write, close, and delete files, as well as how to use file pointers and the difference between relative and absolute paths. Be careful when using write modes to avoid accidental data loss.