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

admin_img Posted By Bajarangi soft , Posted On 31-12-2020

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.

fopen() function

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.

     
Exapmple 1:

<?php 
// Opening a file using fopen() 
// function in read only mode 
$myfile = fopen("/home/gfg.txt", "r") 
                or die("File does not exist!"); 
?> 

Exapmple 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); 
?> 
 

Exapmple 3:

<?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 frea<?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); 
?> 

d($myfile, filesize("gfg.txt")); 
fclose($myfile); 
?> 

Exapmple 4:

<?php 
// Opening a file using fopen() function 
// in read/write mode 
$myfile = fopen("gfg.txt", "w+"); 

// writing to file 
fwrite($myfile, 'companywebdsi=te'); 

// Setting the file pointer to 0th 
// position using rewind() function 
rewind($myfile); 

// writing to file on 0th position 
fwrite($myfile, 'company website'); 
rewind($myfile); 

// displaying the contents of the file 
echo fread($myfile, filesize("gfg.txt")); 
fclose($myfile); 
?> 

Related Post