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.
resource fopen ( $file, $mode, $include_path, $context)
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.
It returns a file pointer resource on success, or FALSE on error.
- 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.
File does not exist!
portal for geeks!
portal for geeks!
- What is the difference between fopen modes “r+”, "rw+" and “w+” in PHP?
- PHP | XMLReader open() Function
- How to delete text from file using preg_replace() function in PHP ?
- How to get file input by selected file name without path using jQuery ?
- What does '<?=' short open tag mean in PHP ?
- AngularJS | ng-open Directive
- How to open a PDF files in web browser using PHP?
- HTML | open Attribute
- HTML | DOM open() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.