Writing files with fwrite and file_put_contents in PHP
📖 Scenario: You are creating a simple PHP script to save user notes into a text file on the server. This is like writing a diary entry or saving a shopping list.
🎯 Goal: Build a PHP script that writes a string into a file using two methods: fwrite and file_put_contents.
📋 What You'll Learn
Create a variable with the filename 'notes.txt'.
Create a variable with the text content to save.
Use fwrite to write the content into the file.
Use file_put_contents to write the content into the file.
Print a confirmation message after writing.
💡 Why This Matters
🌍 Real World
Writing files is common when saving user data, logs, or configuration settings on a server.
💼 Career
Many web developers need to handle file operations safely and efficiently to store and retrieve data.
Progress0 / 4 steps
1
Create filename and content variables
Create a variable called $filename and set it to the string 'notes.txt'. Then create a variable called $content and set it to the string 'Today I learned how to write files in PHP.'.
PHP
Hint
Use = to assign the string values to the variables.
2
Open file and write content using fwrite
Add code to open the file $filename for writing using fopen with mode 'w'. Then use fwrite to write the $content into the file. Finally, close the file with fclose.
PHP
Hint
Use fopen to open the file, then fwrite to write, and fclose to close it.
3
Write content using file_put_contents
Add code to write the $content into the file $filename using the function file_put_contents. This function writes the entire string to the file in one step.
PHP
Hint
Use file_put_contents with the filename and content as arguments.
4
Print confirmation message
Add a print statement to display the message 'File written successfully.' after writing the content.
PHP
Hint
Use print('File written successfully.'); to show the message.
Practice
(1/5)
1. Which PHP function is best for quickly writing a string to a file without manually opening or closing it?
easy
A. fclose
B. fwrite
C. fopen
D. file_put_contents
Solution
Step 1: Understand the purpose of file_put_contents
This function writes data to a file in one step, handling opening and closing automatically.
Step 2: Compare with fwrite
fwrite requires manual file opening and closing, so it's not as quick for simple writes.
Final Answer:
file_put_contents -> Option D
Quick Check:
Quick file write = file_put_contents [OK]
Hint: Use file_put_contents for quick file writes without fopen/fclose [OK]
Common Mistakes:
Confusing fwrite as a standalone write function
Thinking fopen writes data
Assuming fclose writes data
2. Which of the following is the correct syntax to write "Hello" to a file named "greet.txt" using fwrite?
easy
A. $file = fopen('greet.txt'); fwrite($file, 'Hello');
B. $file = fopen('greet.txt', 'w'); fwrite($file, 'Hello'); fclose($file);
C. file_put_contents('greet.txt', 'Hello');
D. fwrite('greet.txt', 'Hello');
Solution
Step 1: Check the correct usage of fwrite
fwrite requires a file handle from fopen opened in write mode.
Step 2: Validate the full sequence
$file = fopen('greet.txt', 'w'); fwrite($file, 'Hello'); fclose($file); opens the file in write mode, writes "Hello", then closes the file properly.
Final Answer:
$file = fopen('greet.txt', 'w'); fwrite($file, 'Hello'); fclose($file); -> Option B
Quick Check:
fopen + fwrite + fclose = correct syntax [OK]
Hint: Always fopen with 'w' before fwrite, then fclose [OK]
The file is opened in 'r' (read) mode, which does not allow writing.
Step 2: Understand fwrite requirements
Writing requires 'w', 'a', or 'r+' mode; 'r' mode causes error on fwrite.
Final Answer:
File opened in read mode, cannot write -> Option A
Quick Check:
Write needs write mode, not 'r' [OK]
Hint: Open file with 'w' or 'a' to write, not 'r' [OK]
Common Mistakes:
Using 'r' mode when writing
Ignoring file mode errors
Assuming fwrite works on read-only handle
5. You want to save user input lines into a file, but only if the input is not empty or whitespace. Which PHP code snippet correctly writes non-empty trimmed lines to "input.txt" using fwrite?
hard
A. $file = fopen('input.txt', 'w'); foreach ($lines as $line) { if (trim($line) !== '') { fwrite($file, $line . "\n"); } } fclose($file);
B. $file = fopen('input.txt', 'w'); foreach ($lines as $line) { fwrite($file, $line . "\n"); } fclose($file);
C. file_put_contents('input.txt', implode("\n", $lines));
D. $file = fopen('input.txt', 'r'); foreach ($lines as $line) { if ($line) { fwrite($file, $line); } } fclose($file);
Solution
Step 1: Check file open mode
$file = fopen('input.txt', 'w'); foreach ($lines as $line) { if (trim($line) !== '') { fwrite($file, $line . "\n"); } } fclose($file); opens file in 'w' mode, allowing writing and overwriting.
Step 2: Validate input filtering
It uses trim($line) !== '' to skip empty or whitespace-only lines before writing.
Step 3: Confirm writing and closing
Writes each valid line with newline and closes file properly.
Final Answer:
Option A code snippet -> Option A
Quick Check:
Trim check + fwrite + 'w' mode = correct [OK]
Hint: Trim input lines and write only non-empty with fwrite [OK]
Common Mistakes:
Not trimming lines before writing
Opening file in 'r' mode when writing
Using file_put_contents without filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.