Current Location: Home> Latest Articles> PHP File Handling Basics: A Comprehensive Guide to PHP File Reading and Writing

PHP File Handling Basics: A Comprehensive Guide to PHP File Reading and Writing

gitbox 2025-06-13

1. Overview

PHP is an open-source server-side scripting language widely used for web development. In PHP, file handling is a common and fundamental task, especially for file reading and writing. This article will explore the basic steps of PHP file operations, helping developers master key techniques for working with files.

2. Reading Files

2.1 Opening Files

In PHP, the fopen()

The first parameter of fopen() is the file name, which can be either an absolute or relative path. The second parameter is the file open mode, with the following common modes:

  • 'r': Open for reading only, file pointer at the beginning of the file
  • 'r+': Open for reading and writing, file pointer at the beginning of the file
  • 'w': Open for writing only, file pointer at the beginning of the file, creates file if not existing
  • 'w+': Open for reading and writing, file pointer at the beginning of the file, creates file if not existing
  • 'a': Open for writing only, file pointer at the end of the file, creates file if not existing
  • 'a+': Open for reading and writing, file pointer at the end of the file, creates file if not existing
  • 'x': Open for writing only, creates and opens file, returns false if the file already exists
  • 'x+': Open for reading and writing, creates and opens file, returns false if the file already exists

For example, the following code opens a file in read-only mode:

$myfile = fopen("example.txt", "r");

Here, $myfile is a resource type value used for subsequent file operations.

2.2 Reading File Content

After opening a file, the fread() function can be used to read the contents of the file. The function prototype is as follows:

string fread(resource $handle, int $length)

The first parameter of fread() is the file resource, and the second parameter is the number of bytes to read. For example, the following code reads the entire content of the file:


$myfile = fopen("example.txt", "r");
echo fread($myfile, filesize("example.txt"));
fclose($myfile);

3. Writing to Files

3.1 Opening Files

When using the fopen() function, you can specify a write mode for file writing operations. For example:

$myfile = fopen("example.txt", "w");

Here, 'w' means write mode, and the file will be created if it does not exist.

3.2 Writing Content

Once the file is opened, the fwrite() function can be used to write content to the file. The function prototype is as follows:

int fwrite(resource $handle, string $string [, int $length])

The $handle parameter is the file resource, $string is the content to be written, and $length is the optional length of the content. For example:


$myfile = fopen("example.txt", "w");
$txt = "Hello world!";
fwrite($myfile, $txt);
fclose($myfile);

4. Closing Files

After reading or writing a file, it is important to close the file using the fclose() function to free up system resources. The function prototype is as follows:

bool fclose(resource $handle)

For example, the following code closes the file that was previously opened:

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

5. Example Code

Here is a complete example demonstrating both file reading and writing operations:


// Open file and read content
$myfile = fopen("example.txt", "r");
echo fread($myfile, filesize("example.txt"));
fclose($myfile);

// Create file and write content
$myfile = fopen("example.txt", "w");
$txt = "Hello world!";
fwrite($myfile, $txt);
fclose($myfile);

6. Conclusion

This article covers the basic steps of PHP file handling, including reading and writing to files. By mastering functions like fopen(), fread(), and fwrite(), developers can easily perform file operations. When working with file handling, be sure to focus on stability and security to prevent malicious tampering or loss of file data.