File open modes tell PHP how you want to use a file, like reading or writing. They help PHP know what to do with the file.
When you want to read data from a file, like reading a list of names.
When you want to write new data to a file, like saving user input.
When you want to add data to the end of an existing file without erasing it.
When you want to create a new file or overwrite an old one.
When you want to update parts of a file without deleting everything.
Syntax
PHP
$file = fopen('filename.txt', 'mode');
The mode is a string that tells PHP how to open the file.
Common modes include 'r' for read, 'w' for write, and 'a' for append.
Examples
Open data.txt for reading only.
PHP
$file = fopen('data.txt', 'r');
Open log.txt to add new data at the end.
PHP
$file = fopen('log.txt', 'a');
Open notes.txt for writing, erasing old content.
PHP
$file = fopen('notes.txt', 'w');
Open report.txt for reading and writing.
PHP
$file = fopen('report.txt', 'r+');
Sample Program
This program creates a file called example.txt, writes "Hello, world!" to it, then reads and prints the content.
PHP
<?php
$filename = 'example.txt';
// Open file for writing (this will create or erase the file)
$file = fopen($filename, 'w');
// Write a line to the file
fwrite($file, "Hello, world!\n");
// Close the file
fclose($file);
// Open file for reading
$file = fopen($filename, 'r');
// Read the content
$content = fread($file, filesize($filename));
// Close the file
fclose($file);
// Show the contentecho $content;
?>
OutputSuccess
Important Notes
Always close a file with fclose() to free system resources.
Using 'w' mode erases the file content if it exists, so be careful.
Use 'a' mode to add data without deleting existing content.
Summary
File open modes tell PHP how to access a file (read, write, append).
Choose the mode based on what you want to do with the file.
Always close files after opening them to avoid problems.
Practice
(1/5)
1. Which PHP file open mode allows you to read from a file without changing its contents?
easy
A. r - Open for reading only
B. w - Open for writing only
C. a - Open for appending
D. x - Create and open for writing
Solution
Step 1: Understand the purpose of each mode
r mode opens a file for reading only, without modifying it. Other modes allow writing or creating files.
Step 2: Match the mode to the requirement
Since the question asks for reading without changing, r is the correct mode.
Final Answer:
r - Open for reading only -> Option A
Quick Check:
Read only = r [OK]
Hint: Read only mode is just 'r' in PHP file functions [OK]
Common Mistakes:
Choosing 'w' which overwrites the file
Confusing 'a' (append) with read mode
Using 'x' which creates a new file
2. Which of the following is the correct syntax to open a file for writing in PHP?
easy
A. fopen('file.txt', 'r')
B. fopen('file.txt', 'a')
C. fopen('file.txt', 'rw')
D. fopen('file.txt', 'w')
Solution
Step 1: Recall the mode for writing
The mode 'w' opens a file for writing and truncates it if it exists.
Step 2: Check syntax correctness
fopen('file.txt', 'w') is the correct syntax to open for writing.
Final Answer:
fopen('file.txt', 'w') -> Option D
Quick Check:
Write mode = 'w' [OK]
Hint: Use 'w' to open file for writing in fopen [OK]
The file is opened in 'r' mode which is read-only and does not allow writing.
Step 2: Understand fwrite limitation
Calling fwrite on a read-only file causes an error because writing is not permitted.
Final Answer:
Cannot write to a file opened in 'r' mode -> Option C
Quick Check:
Write needs 'w' or 'a' mode [OK]
Hint: Write only works if file opened with 'w' or 'a' [OK]
Common Mistakes:
Ignoring mode restrictions
Assuming fclose fixes write errors
Confusing syntax errors with mode errors
5. You want to open a file in PHP to read and write without truncating it, and create it if it doesn't exist. Which mode should you use?
hard
A. w+
B. a+
C. r+
D. x+
Solution
Step 1: Understand each mode's behavior
r+ reads/writes but fails if file missing; w+ truncates file; a+ reads/writes, creates if missing, appends writes; x+ creates new file only.
Step 2: Match requirements
We want read/write, no truncation, and create if missing. a+ fits all: it opens for reading and appending, creates file if missing, and does not truncate.
Final Answer:
a+ -> Option B
Quick Check:
Read/write + create + no truncate = a+ [OK]
Hint: Use 'a+' to read/write and create without truncating [OK]
Common Mistakes:
Choosing 'r+' which fails if file missing
Choosing 'w+' which truncates file
Confusing 'x+' which fails if file exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.