📖 Scenario: You have a text file named quotes.txt that contains several inspirational quotes, one per line. You want to read this file in different ways using PHP to understand how file reading works.
🎯 Goal: Learn how to read a file using fread, fgets, and file functions in PHP by creating a script that reads and displays the contents of quotes.txt step-by-step.
📋 What You'll Learn
Create a file handle to open quotes.txt for reading
Use fread to read the entire file content
Use fgets to read the file line by line
Use file function to read all lines into an array
Print the results after each reading method
💡 Why This Matters
🌍 Real World
Reading files is a common task in web development, such as loading configuration files, logs, or user data stored in text files.
💼 Career
Understanding file reading methods in PHP is essential for backend developers working with file systems, data processing, and server-side scripting.
Progress0 / 4 steps
1
Open the file quotes.txt for reading
Create a variable called file and open quotes.txt in read mode using fopen.
PHP
Hint
Use fopen with the filename "quotes.txt" and mode "r" to open the file for reading.
2
Read the entire file content using fread
Create a variable called content and use fread with $file and filesize("quotes.txt") to read the whole file content.
PHP
Hint
Use fread with the file handle $file and the size of the file using filesize("quotes.txt").
3
Read the file line by line using fgets
Create a variable called lines as an empty array. Use a while loop with fgets($file) to read each line and append it to lines. Close the file after reading.
PHP
Hint
Use rewind($file) to reset the file pointer before reading lines. Then use a while loop with fgets to read each line until the end of the file. Don't forget to close the file with fclose.
4
Read all lines at once using file and print all results
Create a variable called allLines and assign it the result of file("quotes.txt"). Then print content, lines, and allLines using print_r inside <pre> tags for readability.
PHP
Hint
Use file("quotes.txt") to read all lines into an array. Use print_r inside <pre> tags to print content, lines, and allLines clearly.
Practice
(1/5)
1. Which PHP function reads a file line by line, returning one line at a time?
easy
A. fopen()
B. fread()
C. file()
D. fgets()
Solution
Step 1: Understand function behavior
fgets() reads a file one line at a time, useful for line-by-line processing.
Step 2: Compare with other functions
fread() reads bytes, file() reads whole file into array, fopen() opens file but does not read.
Final Answer:
fgets() -> Option D
Quick Check:
Line-by-line reading = fgets() [OK]
Hint: fgets() reads one line at a time from a file [OK]
Common Mistakes:
Confusing fread() with fgets()
Thinking file() reads line by line
Using fopen() to read data directly
2. Which of the following is the correct syntax to read 100 bytes from a file pointer $fp using fread()?
easy
A. fread($fp);
B. fread(100, $fp);
C. fread($fp, 100);
D. fread(100);
Solution
Step 1: Recall fread() parameter order
fread() takes the file pointer first, then the number of bytes to read.
Step 2: Validate syntax correctness
Only fread($fp, 100); matches the correct order and required parameters.
Final Answer:
fread($fp, 100); -> Option C
Quick Check:
fread(fp, bytes) syntax = fread($fp, 100); [OK]
Hint: fread() needs file pointer first, then byte count [OK]
Common Mistakes:
Swapping parameters order
Omitting byte count
Passing only one argument
3. What will be the output of the following PHP code if the file data.txt contains:
Line1
Line2
Line3
Code:
$lines = file('data.txt');
echo trim($lines[1]);
medium
A. Line2
B. Line1
C. Line3
D. Error
Solution
Step 1: Understand file() output
file() reads the whole file into an array, each element is a line including newline.
Step 2: Access second line and trim
$lines[1] is the second line "Line2\n"; trim() removes newline, so output is "Line2".
Final Answer:
Line2 -> Option A
Quick Check:
file()[1] trimmed = Line2 [OK]
Hint: file() returns array of lines, index 1 is second line [OK]
Common Mistakes:
Confusing array index starting at 0
Not trimming newline characters
Expecting fread() output instead
4. Identify the error in this PHP code snippet that tries to read the first 10 bytes of a file: