CodeBlanks Logo Icon CodeBlanks

Summary - File handling

PHP course · Intermediate · Lesson 75/115

Build a journal app with file_get_contents, file_put_contents and fopen.

Summary: the file handling toolkit

In this group you learned two approaches to file handling:

  • One-shot: read the file as a string with file_get_contents() and write a string with file_put_contents(). Ideal for short and simple tasks.
  • Handle-based: open with fopen(), work with fwrite()/fread(), close with fclose(). Used when step-by-step reading or different modes are needed.

Now we will build a small journal app that combines both approaches.

Exercise Journal — one-shot approach

Write two entries to journal.txt. Write the first entry with file_put_contents() as 'Journal started' . PHP_EOL (resets the file). Append the second entry with the append flag as 'Learned PHP today.' . PHP_EOL. Then read the file with file_get_contents() and print it with echo.

Show example solution
Start with file_put_contents($file, 'Journal started' . PHP_EOL);. Then add file_put_contents($file, 'Learned PHP today.' . PHP_EOL, FILE_APPEND);. Finally echo file_get_contents($file);.
Example solution:
<?php
$file = __DIR__ . '/journal.txt';

// Write the first entry
file_put_contents($file, 'Journal started' . PHP_EOL);

// Append the second entry with FILE_APPEND
file_put_contents($file, 'Learned PHP today.' . PHP_EOL, FILE_APPEND);

// Read the file and print it
echo file_get_contents($file);

Recap of the handle-based flow

The handle-based approach has four steps:

  1. Open in the right mode ('r', 'w', 'a') with fopen().
  2. Write with fwrite() or read with fread().
  3. Reopen in a different mode if needed.
  4. Close with fclose().

This flow gives flexibility through mode choice and prevents handles from staying open. Now we will build the same journal with handles.

Exercise Journal — handle-based approach

Open notes.txt with fopen() in 'w' mode and assign the handle to $wr. Use fwrite() to write 'First line' . PHP_EOL, then fwrite() again to write 'Second line' . PHP_EOL. Close with fclose($wr). Then open the file in 'r' mode, assign the handle to $rd, read 50 bytes with fread() into $content, close with fclose($rd) and print with echo $content.

Show example solution
Open with $wr = fopen($file, 'w');. Write fwrite($wr, 'First line' . PHP_EOL); fwrite($wr, 'Second line' . PHP_EOL); fclose($wr);. Then $rd = fopen($file, 'r'); $content = fread($rd, 50); fclose($rd); echo $content;.
Example solution:
<?php
$file = __DIR__ . '/notes.txt';

// Open in write mode
$wr = fopen($file, 'w');

// Write two lines and close
fwrite($wr, 'First line' . PHP_EOL);
fwrite($wr, 'Second line' . PHP_EOL);
fclose($wr);

// Open in read mode
$rd = fopen($file, 'r');

// Read 50 bytes, close and print
$content = fread($rd, 50);
fclose($rd);
echo $content;

Test yourself

  1. Match functions to their jobs

    file_get_contents()
    Reads whole file as a string
    file_put_contents()
    Writes a string to a file
    fopen()
    Opens a file in a specific mode
    fclose()
    Closes the handle
  2. You want to first write to a file and then read it back. What is the correct order?

    • fopen('w') → fwrite → fclose → fopen('r') → fread → fclose
    • fopen('w') → fread → fclose → fopen('r') → fwrite → fclose
    • fopen('r') → fwrite → fread → fclose
    • fopen('a') → fwrite → fread → fclose
    Show answer

    fopen('w') → fwrite → fclose → fopen('r') → fread → fclose — Correct. Open in write mode and close, then open in read mode and close.

All lessons in Intermediate