Reading files lets your program get information stored in text files. This helps you use data saved earlier or from other sources.
Reading files (fread, fgets, file) in PHP
<?php // Open a file for reading $handle = fopen('filename.txt', 'r'); // Read fixed number of bytes $content = fread($handle, 100); // Read one line $line = fgets($handle); // Close the file fclose($handle); // Read entire file into array of lines $lines = file('filename.txt'); ?>
fopen opens the file and returns a handle to use for reading.
fread reads a set number of bytes from the file.
fgets reads one line at a time.
file reads the whole file into an array, each element is a line.
<?php $handle = fopen('data.txt', 'r'); $content = fread($handle, 50); fclose($handle); echo $content; ?>
<?php $handle = fopen('data.txt', 'r'); while (($line = fgets($handle)) !== false) { echo $line; } fclose($handle); ?>
<?php $lines = file('data.txt'); foreach ($lines as $line) { echo $line; } ?>
This program creates a small file, then reads it three ways: first 10 bytes with fread, line by line with fgets, and all lines at once with file. It prints the results so you can see how each method works.
<?php // Sample program to read a file using fread, fgets, and file // Create a sample file file_put_contents('sample.txt', "Hello\nWorld\nPHP File Reading!\n"); // Using fread $handle = fopen('sample.txt', 'r'); $content = fread($handle, 10); // read first 10 bytes fclose($handle); echo "Using fread (10 bytes):\n" . $content . "\n\n"; // Using fgets $handle = fopen('sample.txt', 'r'); $line1 = fgets($handle); // read first line $line2 = fgets($handle); // read second line fclose($handle); echo "Using fgets (line by line):\n" . $line1 . $line2; // Using file $lines = file('sample.txt'); echo "Using file (all lines):\n"; foreach ($lines as $line) { echo $line; } // Clean up sample file unlink('sample.txt');
Always close files with fclose() after reading to free resources.
fread() reads bytes, so it may cut off in the middle of a line.
fgets() reads one line at a time, including the newline character.
Use fread() to read a specific number of bytes from a file.
Use fgets() to read a file line by line.
Use file() to read the whole file into an array of lines easily.
Practice
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 DQuick Check:
Line-by-line reading = fgets() [OK]
- Confusing fread() with fgets()
- Thinking file() reads line by line
- Using fopen() to read data directly
$fp using fread()?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
Onlyfread($fp, 100);matches the correct order and required parameters.Final Answer:
fread($fp, 100); -> Option CQuick Check:
fread(fp, bytes) syntax =fread($fp, 100);[OK]
- Swapping parameters order
- Omitting byte count
- Passing only one argument
data.txt contains:Line1 Line2 Line3
Code:
$lines = file('data.txt');
echo trim($lines[1]);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 AQuick Check:
file()[1] trimmed = Line2 [OK]
- Confusing array index starting at 0
- Not trimming newline characters
- Expecting fread() output instead
$fp = fopen('file.txt', 'r');
$content = fread(10, $fp);
echo $content;Solution
Step 1: Check fread() parameter order
fread()expects file pointer first, then number of bytes. Here they are reversed.Step 2: Confirm other parts
fopen('file.txt', 'r')is valid for reading text files; no third parameter needed; no info about fopen failure.Final Answer:
Parameters of fread() are reversed -> Option BQuick Check:
fread(fp, bytes) order matters [OK]
- Swapping fread() arguments
- Assuming fopen() mode error without evidence
- Expecting extra fread() parameters
Solution
Step 1: Consider memory and line processing
fgets()reads line by line, efficient for large files and allows checking each line.Step 2: Use stripos() for case-insensitive search
Inside the loop,stripos()finds "error" ignoring case, so only matching lines are printed.Final Answer:
Use fgets() in a loop, check each line with stripos() -> Option AQuick Check:
Line-by-line read + case-insensitive check = fgets() + stripos() [OK]
- Using fread() for line filtering
- Reading whole file into memory unnecessarily
- Printing file pointer instead of content
Ready to prove it?
Pass the test and this pattern moves unknown
