The fopen() function in PHP is an inbuilt function which is used to open a file or an URL. It is used to bind a resource to a steam using a specific filename. The filename and mode to be checked are sent as parameters to the fopen() function and it returns a file pointer resource if a match is found and a False on failure. The error output can be hidden by adding an ‘@’ in front of the function name.
Syntax:
resource fopen ( $file, $mode, $include_path, $context)
Parameters Used:
The fopen() function in PHP accepts four parameters.
- $file: It is a mandatory parameter which specifies the file.
- $mode: It is a mandatory parameter which specifies the access type of the file or stream.
It can have the following possible values:- “r”: It represents Read only. It starts at the beginning of the file.
- “r+”: It represents Read/Write.It starts at the beginning of the file.
- “w”: It represents Write only.It opens and clears the contents of file or create a new file if it doesn’t exist.
- “w+”: It represents Read/Write. It opens and clears the contents of file or creates a new file if it doesn’t exist.
- “a”: It represents Write only. It opens and writes to the end of the file or creates a new file if it doesn’t exist.
- “a+”: It represents Read/Write. It preserves the file’s content by writing to the end of the file.
- “x”: It represents Write only. It creates a new file and returns FALSE and an error if the file already exists.
- “x+”: It represents Read/Write.It creates a new file and returns FALSE and an error if file already exists.
- $include_path: It is an optional parameter which is set to 1 if you want to search for the file in the include_path (Ex. php.ini).
- $context: It is an optional parameter which is used to set the behavior of the stream.
Return Value:
It returns a file pointer resource on success, or FALSE on error.
Exceptions:
- When writing to a text file, the correct line-ending character should be used based on the platform.For example Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.
- It is recommended to use the ‘b’ flag when opening files with fopen().
- An error of level E_WARNING is generated if the open fails.
- When safe mode is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.
- If you are unsure whether filename is a file or a directory, you may need to use the is_dir() function before calling fopen() since fopen() function may also succeed when filename is a directory.
Below programs illustrate the fopen() function.
Program 1:
<?php // Opening a file using fopen() // function in read only mode $myfile = fopen("/home/geeks/gfg.txt", "r") or die("File does not exist!"); ?> |
Output:
File does not exist!
Program 2:
<?php // Opening a file using fopen() // function in read/write mode $myfile = fopen("gfg.txt", 'r+') or die("File does not exist!"); $pointer = fgets($myfile); echo $pointer; fclose($myfile); ?> |
Output:
portal for geeks!
Program 3:
<?php // Opening a file using fopen() function // in read mode along with b flag $myfile = fopen("gfg.txt", "rb"); $contents = fread($myfile, filesize($myfile)); fclose($myfile); print $contents; ?> |
Output:
portal for geeks!
Program 4:
<?php // Opening a file using fopen() function // in read/write mode $myfile = fopen("gfg.txt", "w+"); // writing to file fwrite($myfile, 'geeksforgeeks'); // Setting the file pointer to 0th // position using rewind() function rewind($myfile); // writing to file on 0th position fwrite($myfile, 'geeksportal'); rewind($myfile); // displaying the contents of the file echo fread($myfile, filesize("gfg.txt")); fclose($myfile); ?> |
Output:
geeksportalks
Reference:
http://php.net/manual/en/function.fopen.php
Recommended Posts:
- What is the difference between fopen modes “r+”, "rw+" and “w+” in PHP?
- How to Open URL in New Tab using JavaScript ?
- How to open URL in a new window using JavaScript ?
- Convert relative path URL to absolute path URL using JavaScript
- Download file from URL using PHP
- How to download a CSV file in PHP that is triggered through a URL ?
- How to get the file name from page URL using JavaScript ?
- PHP | XMLReader open() Function
- How to open a PDF files in web browser using PHP?
- What does '<?=' short open tag mean in PHP ?
- How to test a URL for 404 error in PHP?
- Get the full URL in PHP
- How to check if URL contain certain string using PHP?
- Program to get the subdomain of a URL using PHP
- Saving an Image from URL in PHP
- How to add http:// if it doesn't exists in the URL in PHP?
- How to get parameters from a URL string in PHP?
- How to check the existence of URL in PHP?
- JavaScript | WebAPI | File | File.type Property
- JavaScript | WebAPI | File | File.size Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

