Open In App

PHP | fopen( ) (Function open file or URL)

Last Updated : 08 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads