← Back to PHP Course | Chapter 9: File Handling | Lesson 2 of 8

PHP Reading Files

Reading a file is like opening a book and going through its pages. PHP opens the file, reads its contents line by line or all at once, then closes it.
Syntax
php
$content = file_get_contents("filename.txt");   // whole file

$handle = fopen("filename.txt", "r");
$line = fgets($handle);   // one line
fclose($handle);

Reading with file_get_contents()

fopen($path, r) opens a file in read mode and returns a resource handle you pass to other file functions, or false if the file doesn't exist or can't be opened, which you should always check.

Example: Reading with file_get_contents()

php
<?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");
// Check whether `$handle`
if ($handle) {
    // Print "File opened for reading" to the output
    echo "File opened for reading";
    // Call `fclose($handle)`
    fclose($handle);
}
?>

Reading Line-by-Line with fgets()

fread($handle, $length) reads up to a given number of bytes from an open file, while fgets($handle) reads just the next line, which is usually the more natural choice for line-oriented text files.

Example: Reading Line-by-Line with fgets()

php
<?php
// Call `file_put_contents("data.txt", "First line\nSecond line")`
file_put_contents("data.txt", "First line\nSecond line");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fread($handle, 5) . "\n"` to the output
echo fread($handle, 5) . "\n";
// Call `fclose($handle)`
fclose($handle);

// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fgets($handle)` to the output
echo fgets($handle);
// Call `fclose($handle)`
fclose($handle);
?>

Reading Character-by-Character with fgetc()

file_get_contents($path) reads an entire file into a single string in one call, which is simpler than manually opening, reading, and closing a handle when you just need the whole file's contents at once.

Example: Reading Character-by-Character with fgetc()

php
<?php
// Call `file_put_contents("data.txt", "Hello World")`
file_put_contents("data.txt", "Hello World");
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>

Reading Files as Arrays with file()

feof($handle) checks whether the read position has reached the end of the file, and is commonly used as the loop condition when reading a file line by line with fgets() inside a while loop.

Example: Reading Files as Arrays with file()

php
<?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 `!feof($handle)` holds
while (!feof($handle)) {
    // Print `fgets($handle)` to the output
    echo fgets($handle);
}
// Call `fclose($handle)`
fclose($handle);
?>

Reading Custom Bytes with fread()

Always close a file handle with fclose() once you're done, or rely on file_get_contents() which handles this automatically, since leaving handles open can exhaust the server's available file descriptors under load.

Example: Reading Custom Bytes with fread()

php
<?php
// Call `file_put_contents("data.txt", "Some content here")`
file_put_contents("data.txt", "Some content here");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fread($handle, 4)` to the output
echo fread($handle, 4);
// Call `fclose($handle)`
fclose($handle);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using file_get_contents on a huge file, which loads it all into memory.
  2. Not checking that the file exists first, which raises a warning.
  3. Forgetting that fgets keeps the trailing newline in each line.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.