CodeBlanks Logo Icon CodeBlanks

Summary - File handling

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.

Practice 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.

Starter code

index.php
<?php
$file = __DIR__ . '/journal.txt';

// Write the first entry

// Append the second entry with the append flag

// Read the file and print it

Sample solution

index.php
<?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);

Expected output

Journal started
Learned PHP today.

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.

Practice 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.

Starter code

index.php
<?php
$file = __DIR__ . '/notes.txt';

// Open in write mode
$wr = null;

// Write two lines and close

// Open in read mode
$rd = null;

// Read 50 bytes, close and print

Sample solution

index.php
<?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;

Expected output

First line
Second line

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match functions to their jobs

Term Meaning
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('r') → fwrite → fread → fclose
  • fopen('w') → fread → fclose → fopen('r') → fwrite → fclose
  • fopen('w') → fwrite → fclose → fopen('r') → fread → fclose Correct answer
  • fopen('a') → fwrite → fread → fclose

Explanation: Open in write mode and close, then open in read mode and close.

All lessons in Intermediate