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

PHP File Operations: Common Methods and Examples for Reading and Writing Files

M66 2025-06-17

PHP File Operations: Reading and Writing Files Techniques

PHP is a widely-used scripting language, commonly used for web development. Reading and writing files are common tasks in web development. This article summarizes some of the most commonly used PHP file reading and writing techniques, and provides corresponding example codes to help developers improve efficiency.

1. File Reading Techniques

  1. Using the file_get_contents() function to read the entire file content:
    Example code:
    $file_content = file_get_contents('example.txt');
    echo $file_content;
                
  2. Using the fgets() function to read file content line by line:
    Example code:
    $file = fopen('example.txt', 'r');
    while (($line = fgets($file)) !== false) {
        echo $line;
    }
    fclose($file);
                
  3. Using the fread() function to read file content by specified byte length:
    Example code:
    $file = fopen('example.txt', 'r');
    $file_content = fread($file, 1024);
    echo $file_content;
    fclose($file);
                
  4. Using the file() function to read file content into an array:
    Example code:
    $file_lines = file('example.txt');
    foreach ($file_lines as $line) {
        echo $line;
    }
                

2. File Writing Techniques

  1. Using the file_put_contents() function to write data to a file:
    Example code:
    $data = "Hello, World!";
    file_put_contents('example.txt', $data);
                
  2. Using the fputs() function to write data to a file:
    Example code:
    $file = fopen('example.txt', 'w');
    $data = "Hello, World!";
    fputs($file, $data);
    fclose($file);
                
  3. Using the fwrite() function to write data to a file:
    Example code:
    $file = fopen('example.txt', 'w');
    $data = "Hello, World!";
    fwrite($file, $data);
    fclose($file);
                
  4. Opening a file in append mode ('a') and writing data:
    Example code:
    $file = fopen('example.txt', 'a');
    $data = "Hello, World!";
    fwrite($file, $data);
    fclose($file);
                

3. Example - Count the Number of Lines in a File

Below is an example using file reading techniques to count the number of lines in a given file:

Example code:
$file = fopen('example.txt', 'r');
$line_count = 0;
while (($line = fgets($file)) !== false) {
    $line_count++;
}
fclose($file);
echo "Total number of lines: " . $line_count;
    

Conclusion

This article introduces several commonly used PHP file reading and writing techniques with code examples. Developers can use functions like `file_get_contents()`, `fgets()`, `fread()`, and `file()` for reading files, and functions like `file_put_contents()`, `fputs()`, and `fwrite()` for writing to files. Mastering these techniques will help improve development efficiency and simplify file operations.