Syntax:
resource fopen ( $file, $mode, $include_path, $context)
Parameters Used:
The fopen() function in PHP accepts four parameters.
<?php
// Opening a file using fopen()
// function in read only mode
$myfile = fopen("/home/gfg.txt", "r")
or die("File does not exist!");
?>
<?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);
?>
<?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);
?>
<?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);
?>