The fopen() is a built-in function of PHP that has been derived from the C programming language. It is used to open a file for creating, reading, writing, or modifying. It is also used to read the content from a URL address. It binds the resource to a stream with a particular filename. The different purposes of using the fopen() function in PHP have been shown in this tutorial.
Syntax
It has two mandatory arguments and two optional arguments.
- The first argument contains the filename that will be opened.
- The second argument contains the mode of opening the file.
- The third argument is used to search the file in the particular path.
- The fourth argument is used to specify the stream context.
The Mode values that can be used in the second argument as mentioned below:
Mode | Purpose |
---|---|
r | It is used to open the file for reading. |
r+ | It is used to open the file for reading and writing. |
w | It is used to open the file for writing. If the file exists, then it will delete the existing content before writing. |
w+ | It is used to open the file for writing and reading. If the file exists, then it will delete the existing content before writing. |
a | It is used to open the file for writing at the end of the file. |
a+ | It is used to open the file for writing at the end of the file and reading. |
x | It used to open a new file for writing. The fopen() function will return false if the file exists. |
x+ | It used to open a new file for writing and reading. The fopen() function will return false if the file exists. |
Uses of the fopen() Function
Different uses of the fopen() function have been shown in this tutorial by using various examples.
Example 1: Open a File for Writing
Create a PHP file with the following script that will create a new file for writing and reading. The first fopen() function of the script has been used to create a file named department.txt for writing only, and a string value will be written in the value. Next, file_get_contents() function is used to read the content of the file. The second fopen() function has been used to open the file for reading and writing. The content of the file will be printed again after adding two string values:
//Set the filename
$filename = "department.txt";
//Open the file for writing only
$file_handler = fopen($filename,"w");
//Write a string value in the file
fwrite($file_handler,"CSE ");
//Close the file
fclose($file_handler);
//Print the content of the file
echo "<b>The current content of the file:</b><br />".file_get_contents($filename);
//Open the file for writing and reading
$file_handler = fopen($filename,"w+");
//Write the first string value in the file
fwrite($file_handler,"BBA ");
//Write the second string value in the file
fwrite($file_handler,"English ");
//Close the file
fclose($file_handler);
//Print the content of the file
echo "<br /><br /><b>The current content of the file:</b><br />".file_get_contents($filename);
?>
Output:
The following output will appear after executing the previous script:
Example 2: Open a File for Reading
Create a PHP file with the following script that will open an existing file for reading and writing. Here, the fread() function has been used to read the content of the file.
//Set the filename
$filename = "department.txt";
if(file_exists($filename))
{
//Open the file for writing only
$file_handler = fopen($filename,"r");
//Print the content of the file
echo "<b>The current content of the file:</b><br />".fread($file_handler,filesize($filename));
//Close the file
fclose($file_handler);
//Open the file for writing only
$file_handler = fopen($filename,"r+");
//Write a string value in the file
fwrite($file_handler,"EEE ");
//Close the file
fclose($file_handler);
//Print the content of the file
echo "<br /><br /><b>The current content of the file:</b><br />".file_get_contents($filename);
}
else
echo "File does not exist."
?>
Output:
The following output will appear after executing the previous script:
Example 3: Open a File for Appending
Create a PHP file with the following script that will open an existing file for appending and reading. Here, the file_get_contents() function has been used to read the content of the file.
//Set the filename
$filename = "department.txt";
if(file_exists($filename))
{
//Open the file for writing only
$file_handler = fopen($filename,"a");
//Write a string value in the file
fwrite($file_handler,"Civil ");
//Close the file
fclose($file_handler);
//Print the content of the file
echo "<br /><br /><b>The current content of the file:</b><br />".file_get_contents($filename);
//Open the file for writing only
$file_handler = fopen($filename,"a+");
//Write a string value in the file
fwrite($file_handler,"Pharmacy ");
//Close the file
fclose($file_handler);
//Print the content of the file
echo "<br /><br /><b>The current content of the file:</b><br />".file_get_contents($filename);
}
else
echo "File does not exist."
?>
Output:
The following output will appear after executing the previous script:
Example 4: Open a URL Address for Reading
Create a PHP file with the following script that will open an existing local and remote URL address for reading. Here, the fread() function has been used to read the content of the URL address. If the content of the remote URL address is large in size, then you have to increase the value of the memory_limit in the php.ini file:
//Initialize the variable with empty string
$local_page = '';
//Open handler for reading page of local URL
$fh = fopen('http://localhost/code/array3.php','r') or die("Error occurred.");
//Read the content of the page
while (! feof($fh))
{
$local_page .= fread($fh,5000);
}
fclose($fh);
//Print the content of the page
echo "<b>The output of the local page:</b><br />".$local_page;
//Initialize the variable with empty string
$remote_page = '';
//Open handler for reading page of a valid URL address
$fh = fopen('https://linuxhint.com/bash_comments/','r') or die("Error occurred.");
//Read the content of the online page
while (! feof($fh))
{
$remote_page .= fread($fh,500000000);
}
fclose($fh);
//Print the content of the online page
echo "<br /><b>The output of the remote page:</b><br /><br />".$remote_page;
?>
Output:
The following similar output will appear after executing the previous script.
Conclusion
The uses of the fopen() function with the different mode values have been explained in this tutorial using various examples. I hope the new PHP users will understand the purpose of using the fopen() function and be able to use it properly in their script after reading this tutorial. Please check out LinuxHint for more informative articles.