PHP File Open/Read
In this page:
$handle = fopen("filename.txt", "r");
$line = fgets($handle); // read one line
fclose($handle);
$content = file_get_contents("filename.txt");
Opening a File with fopen()
fopen($filename, $mode) opens the given file and returns a resource handle representing that open file -- "r" mode opens it for reading only, positioned at the very start of the file, ready for the read functions that follow.
Example: Opening a File with fopen()
<?php
// Call `file_put_contents("data.txt", "Sample content")`
file_put_contents("data.txt", "Sample content");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `$handle ? "File opened" : "Failed"` to the output
echo $handle ? "File opened" : "Failed";
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
Reading the Whole File with file_get_contents()
file_get_contents($filename) reads an entire file into a single string in one call, without needing fopen(), a read loop, or fclose() at all -- the simplest option when you just need the whole file's contents at once and the file is not too large.
Example: Reading the Whole File with file_get_contents()
<?php
// Call `file_put_contents("data.txt", "Full file contents")`
file_put_contents("data.txt", "Full file contents");
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>
Login to try C/C++/Java/PHP code in the editor
Reading Line by Line with fgets()
fgets($handle) reads a single line from an open file handle, moving the file pointer to the start of the next line each time it is called -- looping fgets() until it returns false is the standard way to process a file one line at a time without loading it all into memory at once.
Example: Reading Line by Line with fgets()
<?php
// Call `file_put_contents("data.txt", "Line 1\nLine 2\nLine 3")`
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Keep looping while `($line = fgets($handle)) !== false` holds
while (($line = fgets($handle)) !== false) {
// Print `$line` to the output
echo $line;
}
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
Reading a File into an Array with file()
file($filename) reads an entire file and returns it as an array, with each element holding one line -- combining the simplicity of file_get_contents() with the line-by-line structure of fgets(), useful when you want to loop over lines using foreach.
Example: Reading a File into an Array with file()
<?php
// Call `file_put_contents("data.txt", "Line 1\nLine 2\nLine 3")`
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
// Declare `$lines`, set to `file("data.txt")`
$lines = file("data.txt");
// Loop over `$lines`, binding each item to `$line`
foreach ($lines as $line) {
// Print `$line` to the output
echo $line;
}
?>
Login to try C/C++/Java/PHP code in the editor
Reading a Specific Number of Bytes with fread()
fread($handle, $length) reads up to $length bytes from an open file handle, useful when you need precise control over how much data is read at once -- often paired with filesize() to read an entire binary or text file in one controlled call.
Example: Reading a Specific Number of Bytes with fread()
<?php
// Call `file_put_contents("data.txt", "Hello World")`
file_put_contents("data.txt", "Hello World");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Declare `$size`, set to `filesize("data.txt")`
$size = filesize("data.txt");
// Print `fread($handle, $size)` to the output
echo fread($handle, $size);
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting to check whether fopen() actually succeeded (it returns false on failure) before trying to read from the handle it returned.
- Opening a file in the wrong mode -- like "w" (write, which truncates the file) when you only meant to read it with "r".
- Forgetting to call fclose() when done, leaving the file handle open longer than necessary and wasting server resources on long-running scripts.
- fopen($path, $mode) opens a file and returns a resource handle, or false if the file could not be opened.
- fread(), fgets(), and file_get_contents() each read file contents in a slightly different way, suited to different situations.
- Always close a file handle with fclose() once you are done reading from it.
File-reading functions have been part of PHP since its earliest versions and behave consistently across every server environment with filesystem access.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: