Current Location: Home> Latest Articles> PHP File Handling Tutorial: How to Read and Write Files in PHP

PHP File Handling Tutorial: How to Read and Write Files in PHP

gitbox 2025-06-17

PHP File Handling Tutorial: How to Read and Write Files in PHP

PHP is a popular open-source scripting language widely used for web development. With PHP, developers can easily read and write files to store and manage data. This article will introduce how to perform file operations in PHP, including opening, reading, writing, and closing files.

1. Opening and Closing Files

In PHP, the `fopen()` function is used to open a file. This function takes two parameters: the file name and the mode for opening. The mode defines how the file can be accessed, such as reading, writing, or appending. Here are the common file opening modes:

  • "r" - Read only
  • "w" - Write only
  • "a" - Append mode
  • "x" - Create and write
  • "r+" - Read and write
  • "w+" - Read and write, file content is cleared
  • "a+" - Append and read/write
  • "x+" - Create, read and write

1.1 Opening a File

Use the `fopen()` function to open a file with the desired mode, as shown in the example below:

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

This code opens the file "file.txt" and sets it to read-only mode.

1.2 Closing a File

To avoid resource leakage or data corruption, it is important to close a file after finishing operations. You can use the `fclose()` function to close an open file, as shown below:

fclose($file_handle);

Closing the file releases the system resources associated with it.

2. Reading a File

2.1 Reading the Entire File

To read the entire content of a file into a PHP variable, you can use the `fread()` function, as shown below:

$file_handle = fopen("file.txt", "r");
$file_content = fread($file_handle, filesize("file.txt"));
fclose($file_handle);

This code opens the "file.txt" file and reads its content into the `$file_content` variable.

2.2 Reading a File Line by Line

If you need to read a file line by line, use the `fgets()` function, as shown below:

$file_handle = fopen("file.txt", "r");
while (!feof($file_handle)) {
    $line = fgets($file_handle);
    // Process each line of data
}
fclose($file_handle);

This code reads the file line by line until the end of the file is reached.

3. Writing to a File

To write data to a file, you can use the `fwrite()` function, as shown below:

$file_handle = fopen("file.txt", "w");
fwrite($file_handle, "Write some text here");
fclose($file_handle);

This code opens the "file.txt" file and writes the text "Write some text here" into it.

3.1 Appending Data to a File

If you want to append data to the end of a file, you can use the append mode `a`, as shown below:

$file_handle = fopen("file.txt", "a");
fwrite($file_handle, "Append some text here");
fclose($file_handle);

This code appends the text to the end of the file.

4. Checking if a File Exists

You can use the `file_exists()` function to check if a file already exists, as shown below:

if (file_exists("file.txt")) {
    echo "file.txt exists";
} else {
    echo "file.txt does not exist";
}

This code checks if the "file.txt" file exists and outputs the corresponding message.

5. Deleting a File

To delete a file, you can use the `unlink()` function, as shown below:

unlink("file.txt");

This code deletes the "file.txt" file.

6. Conclusion

This article covered how to perform file operations in PHP, including opening, reading, writing, and closing files. PHP provides several functions to handle files efficiently, and developers should be familiar with these functions to manage files effectively.