Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP File Handling
PHP Intermediate FREE

File Handling

Lesson 1 of 17 Intermediate Interactive

PHP provides many functions for reading, writing, and managing files on the server. File handling is essential for log files, data storage, and file uploads.

Reading Files

  • file_get_contents() — Read entire file into a string (simplest method)
  • fopen() — Open a file for reading/writing
  • fread() — Read specific number of bytes
  • fgets() — Read a single line

Writing Files

  • file_put_contents() — Write a string to a file (creates if needed)
  • fwrite() — Write to an open file handle

File Operations

  • file_exists() — Check if a file exists
  • is_file() — Check if it's a file (not a directory)
  • unlink() — Delete a file
  • rename() — Rename a file

Warning: Always sanitize file paths to prevent directory traversal attacks.

Syntax

PHP
<?php
// Read a file

$content = file_get_contents("data.txt");
echo $content;

// Write to a file

file_put_contents("log.txt", "Entry at " . date("Y-m-d"));

// Append to a file

file_put_contents("log.txt", "New entry
", FILE_APPEND);

// Check if file exists

if (file_exists("data.txt")) {
    echo "File found!";
}

// Delete a file

unlink("temp.txt");
?>
File Operations
PHP
<?php
$file = sys_get_temp_dir() . "/nx_example.txt";

// Write to file
file_put_contents($file, "Hello, World!
Line two
");

// Read the whole file
echo file_get_contents($file);

// Read line by line
foreach (file($file) as $line) {
    echo "Line: " . trim($line) . "
";
}

unlink($file);
?>

Practice

1
Exercise

Write a PHP script that creates a file called "todo.txt" with 3 todo items and then reads them back.

Answer
<?php
$todos = ["Learn PHP", "Practice Laravel", "Build a project"];
file_put_contents("todo.txt", implode("
", $todos));

$content = file_get_contents("todo.txt");
echo nl2br($content);
?>
2
Exercise

Check if a file exists and display its size, or show an error message if it doesn't.

Answer
<?php
$file = "data.txt";
if (file_exists($file)) {
    echo "Size: " . filesize($file) . " bytes";
} else {
    echo "File not found!";
}
?>

Quick Quiz

1

Which function reads an entire file into a string?

file_get_contents() reads the entire file content into a string. It's the simplest way to read a file.

2

What flag lets you append to a file with file_put_contents()?

FILE_APPEND flag makes file_put_contents() add content to the end instead of overwriting.

3

How do you delete a file in PHP?

unlink() deletes a file from the filesystem.

Interview Questions

file_get_contents() reads the entire file at once — simple but uses more memory for large files. fopen()/fread() opens the file in chunks — better for large files because you control how much data is read. Use file_get_contents() for small files, fopen()/fread() for large files or streaming.

Sanitize file paths by using basename() to strip directory information, validate against a whitelist of allowed directories, use realpath() to resolve the actual path, and never use user input directly in file paths without validation. This prevents attackers from accessing files outside intended directories.