PHP File Open/Read
In this page:
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.
Note: Always store the return value of fopen() and check it is not false before using it, since a missing file or permission problem causes fopen() to fail silently otherwise.
Warning: Opening a file that does not exist in "r" mode returns false rather than throwing an exception, so an unchecked fopen() failure often surfaces later as a confusing error on the next line.
Example: Opening a File with fopen()
<?php
file_put_contents("data.txt", "Sample content");
$handle = fopen("data.txt", "r");
echo $handle ? "File opened" : "Failed";
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.
Note: Use file_get_contents() as your default for smaller files where loading everything into memory at once is not a concern.
Warning: file_get_contents() loads the entire file into memory in one go, which can be a problem for very large files -- fopen() with a read loop is more appropriate there.
Example: Reading the Whole File with file_get_contents()
<?php
file_put_contents("data.txt", "Full file contents");
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.
Note: Prefer fgets() in a loop over file_get_contents() for very large files, since it only holds one line in memory at a time.
Warning: fgets() returns false once the end of the file is reached -- forgetting this exit condition in a while loop can cause the loop to run forever.
Example: Reading Line by Line with fgets()
<?php
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
$handle = fopen("data.txt", "r");
while (($line = fgets($handle)) !== false) {
echo $line;
}
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.
Note: Pass the FILE_IGNORE_NEW_LINES flag to file() if you do not want each array element to include its trailing newline character.
Warning: Like file_get_contents(), file() loads the whole file into memory at once as an array, so it is not ideal for extremely large files.
Example: Reading a File into an Array with file()
<?php
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
$lines = file("data.txt");
foreach ($lines as $line) {
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.
Note: Use fread($handle, filesize($filename)) as a manual alternative to file_get_contents() when you specifically need the open file handle for other operations too.
Warning: Requesting more bytes with fread() than the file actually contains simply reads to the end of the file -- it does not raise an error.
Example: Reading a Specific Number of Bytes with fread()
<?php
file_put_contents("data.txt", "Hello World");
$handle = fopen("data.txt", "r");
$size = filesize("data.txt");
echo fread($handle, $size);
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: