PHP File Open/Read
Reading data from files in PHP
📖 What is File Opening and Reading?
Opening and reading files in PHP lets you access stored data from text files, logs, or configuration files. You can read entire files at once or process them line by line for better memory management.
<?php
// Quick way to read a file
$content = file_get_contents("info.txt");
echo $content;
?>
File Reading Methods
file_get_contents()
The fastest way to read an entire file into a string. Perfect for small to medium files when you need all content at once.
<?php
$data = file_get_contents("file.txt");
echo $data;
?>
fread()
Read a specific number of bytes from a file. Useful when you need precise control over how much data to read at once.
<?php
$file = fopen("data.txt", "r");
$content = fread($file, 100);
fclose($file);
?>
fgets()
Read one line at a time from a file. Ideal for processing large files line by line without loading everything into memory.
<?php
$file = fopen("list.txt", "r");
$line = fgets($file);
fclose($file);
?>
file()
Read entire file into an array where each line becomes an array element. Great for processing lists or structured data.
<?php
$lines = file("names.txt");
foreach($lines as $line) {
echo $line;
}
?>
🔹 Using file_get_contents()
The simplest way to read a complete file:
<?php
// Read entire file
$filename = "welcome.txt";
if(file_exists($filename)) {
$content = file_get_contents($filename);
echo $content;
} else {
echo "File not found!";
}
?>
Output (if welcome.txt contains "Hello, welcome to PHP!"):
Hello, welcome to PHP!
🔹 Opening Files with fopen()
Use fopen() for more control over file reading:
🔸 Basic File Opening
<?php
// Open file for reading
$file = fopen("document.txt", "r");
if($file) {
echo "File opened successfully!";
fclose($file);
} else {
echo "Unable to open file!";
}
?>
🔸 Reading with fread()
<?php
$filename = "story.txt";
$file = fopen($filename, "r");
// Read entire file
$content = fread($file, filesize($filename));
fclose($file);
echo $content;
?>
Important: Always Close Files!
Use fclose() to close files after reading to free up system resources.
🔹 Reading Line by Line
Process large files efficiently by reading one line at a time:
🔸 Using fgets()
<?php
$file = fopen("users.txt", "r");
// Read until end of file
while(!feof($file)) {
$line = fgets($file);
echo $line . "<br>";
}
fclose($file);
?>
Output (if users.txt has 3 names):
John Doe
Jane Smith
Bob Johnson
🔸 Using file() Function
<?php
// Read file into array (each line is an element)
$lines = file("tasks.txt");
foreach($lines as $lineNumber => $line) {
echo "Line " . ($lineNumber + 1) . ": " . $line . "<br>";
}
?>
🔹 Reading Specific Bytes
Read only a portion of a file:
<?php
$file = fopen("article.txt", "r");
// Read first 50 characters
$preview = fread($file, 50);
echo $preview . "...";
fclose($file);
?>
Output:
This is the beginning of a long article about...
🔹 Reading Character by Character
Use fgetc() to read one character at a time:
<?php
$file = fopen("sample.txt", "r");
// Read 10 characters
for($i = 0; $i < 10; $i++) {
$char = fgetc($file);
echo $char;
}
fclose($file);
?>
🔹 Practical Example: Reading CSV File
Read and display data from a CSV file:
<?php
$file = fopen("students.csv", "r");
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Age</th><th>Grade</th></tr>";
while(($data = fgetcsv($file)) !== FALSE) {
echo "<tr>";
echo "<td>" . $data[0] . "</td>";
echo "<td>" . $data[1] . "</td>";
echo "<td>" . $data[2] . "</td>";
echo "</tr>";
}
echo "</table>";
fclose($file);
?>
Output:
| Name | Age | Grade |
|---|---|---|
| Alice | 20 | A |
| Bob | 22 | B |
🔹 Error Handling When Reading Files
Always check if files exist and can be read:
<?php
$filename = "config.txt";
// Check if file exists
if(!file_exists($filename)) {
die("Error: File does not exist!");
}
// Check if file is readable
if(!is_readable($filename)) {
die("Error: File is not readable!");
}
// Safe to read
$content = file_get_contents($filename);
echo $content;
?>