Files
developer-roadmap/roadmaps/php/content/reading-files@S9wTlkbv9-R6dohhZ47hs.md
T
Arik Chakma 274082fd77 feat: clean roadmap content and sync scripts (#10181)
* refactor: clean to roadmap content

* refactor: move shared helpers into scripts/lib
2026-07-28 05:45:17 +06:00

829 B

Reading Files

Reading files is a common task in PHP and it provides a range of functions for this purpose. You can use the fopen() function with the 'r' mode to open a file for reading. The fgets() function lets you read a file line by line, while fread() reads a specified number of bytes. For reading the entire file in one go, use file_get_contents(). Remember to always close the file after you're done with fclose().

Here's a small example using fgets():

$file = fopen("example.txt", "r"); 
if ($file) {
    while (($line = fgets($file)) !== false) {
        echo $line;
    }
    fclose($file);
} else {
    echo 'Error opening file';
}

Visit the following resources to learn more: