Current Location: Home> Latest Articles> PHP File Reading and Writing Tutorial: Basic Methods and Techniques

PHP File Reading and Writing Tutorial: Basic Methods and Techniques

gitbox 2025-06-17

Basic Methods of File Reading and Writing in PHP

PHP is a widely-used server-side scripting language that can be used for developing web applications and websites. File reading and writing are common tasks in PHP. In this article, we will introduce the basic methods for reading and writing files in PHP, along with some example code.

1. File Reading

In PHP, file reading is typically done using the fopen() function to open the file, followed by either fgets() or fread() to read the content of the file.

1.1 Using fgets() to Read File

Here is an example of using fgets() to read file content line by line:

$filename = "example.txt"; // File name
$file = fopen($filename, "r"); // Open file in read-only mode

if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line; // Output file content
    }
    fclose($file); // Close file handle
} else {
    echo "Failed to open file!";
}
    

The above code opens the file using fopen(), reads each line with fgets(), and outputs it. The fgets() function reads the content line by line until the end of the file.

1.2 Using fread() to Read the Entire File

Another common file reading method is to use fread(), which can read the entire content of the file in one go. Here’s an example of using fread():

$filename = "example.txt"; // File name
$file = fopen($filename, "r"); // Open file in read-only mode

if ($file) {
    $content = fread($file, filesize($filename)); // Read entire file content
    echo $content; // Output file content
    fclose($file); // Close file handle
} else {
    echo "Failed to open file!";
}
    

In this example, we first use the filesize() function to get the file size and then read the entire content with fread().

2. File Writing

File writing in PHP typically involves opening the file with fopen() and then using fwrite() to write data to the file.

2.1 Using fwrite() to Write to a File

Here’s a simple example of writing to a file:

$filename = "example.txt"; // File name
$file = fopen($filename, "w"); // Open file in write mode

if ($file) {
    fwrite($file, "Hello, world!"); // Write content
    fclose($file); // Close file handle
} else {
    echo "Failed to open file!";
}
    

The above code opens the file using fopen() in write mode, writes content using fwrite(), and then closes the file handle. If the file doesn’t exist, PHP will automatically create it.

2.2 Using "a" Mode to Append Content

If you want to append content to the end of the file, you can use the "a" mode:

$filename = "example.txt"; // File name
$file = fopen($filename, "a"); // Open file in append mode

if ($file) {
    fwrite($file, "Append content."); // Append content
    fclose($file); // Close file handle
} else {
    echo "Failed to open file!";
}
    

In this example, the file is opened with "a" mode, which appends content to the end of the file. If the file doesn’t exist, it will be created and the content will be appended.

3. Considerations When Working with Files

When reading and writing files in PHP, here are a few important things to keep in mind:

  • Always use fclose() to close the file handle, ensuring that the file content is saved and resources are freed.
  • When writing to a file, be cautious not to overwrite existing content unless that is the intended behavior. Use "a" mode to append data.
  • Ensure that file permissions are correctly set, especially for sensitive files. You can adjust file permissions using the chmod() function.

4. Conclusion

In this article, we introduced the basic methods of file reading and writing in PHP, including how to use fopen(), fgets(), fread(), and fwrite() to read and write file content. Developers need to be mindful of closing file handles and setting appropriate file permissions to ensure secure and efficient file operations.