Working with Files
Read from and write to files using PHP.
Working with Files is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Working with Files
Welcome to the lesson on working with files! In this lesson, you’ll learn how to read, write, and manipulate files in PHP. These skills are essential for creating applications that handle user uploads, configuration files, and logs. Let’s get started!

2
Why Work with Files?
File handling is crucial for many web applications. You can use files to:
- Store and retrieve data.
- Log application events.
- Manage user uploads like images and documents.
Key Point: PHP provides built-in functions to handle files easily and efficiently.
3
Opening a File
To work with a file, you must first open it using the fopen() function. Example:
Modes:
r: Read only.w: Write only; truncates the file or creates a new one.a: Append; writes to the end of the file or creates a new one.
<?php
$file = fopen("example.txt", "r"); // Open the file in read mode
if ($file) {
echo "File opened successfully!";
fclose($file); // Close the file
} else {
echo "Failed to open the file.";
}
?>
4
Reading a File
Use the fread() function to read the contents of a file. Example:
Key Point: Always use filesize() to read the exact size of the file.
<?php
$file = fopen("example.txt", "r");
if ($file) {
$contents = fread($file, filesize("example.txt"));
echo "File Contents: <br>" . nl2br($contents); // Converts newlines to <br> for HTML display
fclose($file);
} else {
echo "Failed to read the file.";
}
?>
5
Writing to a File
Use the fwrite() function to write data to a file. Example:
Tip: Opening a file in w mode overwrites existing content.
<?php
$file = fopen("example.txt", "w");
if ($file) {
fwrite($file, "Hello, world!\nThis is a new line of text.");
echo "Data written successfully!";
fclose($file);
} else {
echo "Failed to write to the file.";
}
?>
6
Appending Data to a File
To add data to the end of a file, open it in append mode (a). Example:
Key Point: Append mode preserves existing content in the file.
<?php
$file = fopen("example.txt", "a");
if ($file) {
fwrite($file, "\nThis line is appended.");
echo "Data appended successfully!";
fclose($file);
} else {
echo "Failed to append data to the file.";
}
?>
7
Deleting a File
Use the unlink() function to delete a file. Example:
Tip: Always check if the file exists before trying to delete it.
<?php
$file = "example.txt";
if (file_exists($file)) {
unlink($file);
echo "File deleted successfully!";
} else {
echo "File does not exist.";
}
?>
8
Checking for File Existence
Use the file_exists() function to check if a file exists before performing operations. Example:
Key Point: Checking for file existence prevents runtime errors.
<?php
$file = "example.txt";
if (file_exists($file)) {
echo "The file exists.";
} else {
echo "The file does not exist.";
}
?>
9
10
Great Job!
Congratulations! You’ve learned how to handle files in PHP, including reading, writing, appending, and deleting files. These skills are essential for building dynamic web applications. In the next lesson, we’ll explore advanced file handling techniques, such as working with file uploads. Let’s keep coding!

Frequently asked questions
Is the “Working with Files” lesson free?
Yes — the full text of “Working with Files” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Working with Files”?
Read from and write to files using PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Working with Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.