PHP Filesystem fopen() Function
The PHP Filesystem fopen() function is used to open a file or URL. If it fails, it can return false and an error on failure. We can hide the error output by adding an '@' in front of the function name. The fopen() function can bind named resource specified by filename to a stream.
Syntax
Below is the syntax of the PHP Filesystem fopen() function −
resource fopen ( string $filename , string $mode [, bool $use_include_path = FALSE [, resource $context ]] )
Parameters
Here are the required and optional parameters of the fopen() function −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
filename(Required) The path to the file you want to open. |
| 2 |
mode(Required) The mode in which you want to open the file like - r, w, a, x, r+, w+, a+. |
| 3 |
use_include_path(Required) Allows to include the search in the include_path. By default, it is set to FALSE. |
| 4 |
resource(Required) A resource which specifies the context of the file handle. |
Return Value
It returns a resource handle if the file was successfully opened, or FALSE on failure.
PHP Version
The fopen() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
In the below code we will see basic usage of the PHP Filesystem fopen() function like how we can open the file and read that file, how we can write in the file after opening.
<?php
$file = fopen("/PhpProject/sample.txt", "r");
$file = fopen("/PhpProject/index.php", "r");
$file = fopen("/PhpProject/sample1.txt", "wb");
?>
Example
In the below PHP code, we will open a file called "myfile.txt" in read mode ("r") using fopen() function. And we will use if-else statement to check whether the file is opened or not. Also we read the file line by line until we reach the end of it with the help of a while loop and fgets().
<?php
$file = fopen("/PhpProject/myfile.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
echo $line;
}
fclose($file);
} else {
echo "Failed to open file.";
}
?>
Output
This will produce the following result −
This is a sample file.
Example
In this example we will use fopen() to open or create a file called "newfile.txt" in writing mode w.
<?php
$file = fopen("newfile.txt", "w");
if ($file) {
fwrite($file, "Hello, world!");
fclose($file);
echo "Data written to file.";
} else {
echo "Failed to open file.";
}
?>
Output
This will lead to the following outcome −
Data written to file.
Example
In this example we will use fopen() to open or create a file called "log.txt" in append mode "a". If the file is successfully opened or created so we will use fwrite() to append the string to the file.
<?php
$file = fopen("log.txt", "a");
if ($file) {
fwrite($file, "New log entry");
fclose($file);
echo "New entry added to log file.";
} else {
echo "Failed to open file.";
}
?>
Output
This will generate the below result −
New entry added to log file.
Note
Checking whether fopen() gives false is important to handle issues correctly. Always use fclose() to close the file handle and release resources after you have completed any file operations.
Summary
The fopen() function in PHP can be used to open files and URLs. Programmers can add, read from, and write to files with the help of this flexible function to work with Filesystems.