Reading from Files in PHP:
PHP provides various functions to read data from files. The most common approach involves the use of file handling functions such as fopen
, fgets
, fread
, and fclose
.
Opening a File:
To open a file, you can use the fopen
function. It takes two parameters: the file name/path and the mode in which you want to open the file (read, write, append, etc.).
$filename = "example.txt";
$handle = fopen($filename, "r"); // "r" mode for reading
if ($handle === false) {
die("Unable to open file: $filename");
}
?>
Reading Line by Line:
The fgets
function reads a single line from the file pointer.
while (!feof($handle)) {
$line = fgets($handle);
echo $line;
}
?>
Reading Entire File Content:
The fread
function reads a specified number of bytes from the file.
$contents = fread($handle, filesize($filename));
echo $contents;
?>
Closing the File:
Always remember to close the file after you've finished reading from it using the fclose
function.
fclose($handle);
?>
Example: Reading from a File:
Let's say we have a file named "example.txt" with the following content:
Line 2
Line 3
$filename = "example.txt";
$handle = fopen($filename, "r");
if ($handle === false) {
die("Unable to open file: $filename");
}
while (!feof($handle)) {
$line = fgets($handle);
echo $line;
}
fclose($handle);
?>
This PHP code will read each line from the file "example.txt" and output it to the browser.
Writing to Files in PHP:
PHP also provides functions to write data to files. You can use fopen
with the modes "w" (write) or "a" (append) to open files for writing.
Opening a File for Writing:
$filename = "example.txt";
$handle = fopen($filename, "w"); // "w" mode for writing
if ($handle === false) {
die("Unable to open file: $filename");
}
?>
Writing Data to a File:
You can use the fwrite
function to write data to the opened file.
$data = "Hello, World!";
fwrite($handle, $data);
?>
Appending Data to a File:
To append data to an existing file, open the file in "a" mode.
$data = "Appending new content!";
fwrite($handle, $data);
?>
Closing the File:
Don't forget to close the file after writing.
fclose($handle);
?>
Example: Writing to a File:
Let's create a PHP script to write some content to a file named "example.txt".
$filename = "example.txt";
$handle = fopen($filename, "w");
if ($handle === false) {
die("Unable to open file: $filename");
}
$data = "Hello, World!";
fwrite($handle, $data);
fclose($handle);
?>
This script will create a new file "example.txt" if it doesn't exist and write the text "Hello, World!" to it.
Error Handling:
Always handle potential errors when working with files, such as checking if the file exists, verifying permissions, and handling exceptions gracefully.
$filename = "example.txt";
// Check if file exists
if (!file_exists($filename)) {
die("File does not exist: $filename");
}
// Check if file is readable
if (!is_readable($filename)) {
die("File is not readable: $filename");
}
// Open file
$handle = fopen($filename, "r");
if ($handle === false) {
die("Unable to open file: $filename");
}
// Read or write operations...
// Close file
fclose($handle);
?>
Practice Excercise Practice now