PHP fopen() Function | How to write fopen Function open file or URL

PHP fopen() function is an inbuilt function that is used to open a file or an URL. basically, it is used to bind a resource to steam using a specific filename. The filename and mode to be checked are sent as parameters to the PHP fopen() function and it returns a file pointer resource, furthermore, if a match is found and a False on failure. Therefore, error output can be hidden by adding an ‘@’ in front of the function name.

.
Important Note: The fopen() function in PHP opens a file or URL.
When writing to a text file in PHP, be sure to use the correct line-ending character! Windows systems use \r\n, Unix systems use \n, and Macintosh systems use \r as the line ending character. furthermore, Windows typically offers a translation flag (‘t’) which will translate \n to \r\n when working with the file. therefore, you can also use ‘b’ to force binary mode. also, to use these flags, specify or prescribe either ‘b’ or ‘t’ as the last character of the mode parameter.

Syntax:

resource fopen ( $file, $mode, $include_path, $context)

Parameter Used

There is the following fopen() function in PHP that accepts four parameters.

Value Description
$file It’s a mandatory/compulsory parameter that specifies the file.
$mode It’s a compulsory parameter that 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/start of the file.
  • “r+”: It represents Read/Write. It starts at the beginning of the file.
  • “w”: It represents Write only. therefore, it opens & clears the contents of the file or creates a new file if it does not exist.
  • “w+”: It represents Read/Write. It opens & clears the contents of the file or creates a new file if it does not exist.
  • “a”: It represents Write only. It writes and opens to the end of the file or creates a new file if it does not exist.
  • “a+”: It represents Write/Read. furthermore, it preserves the file’s content by writing to the end of the file.
  • “x”: It represents Write only. It also creates a new file and also returns FALSE and an error if the file already exists.
  • “x+”: It represents Write/Read. It will create a new file and also returns FALSE and an error if the file already exists.
$include_path It is an optional parameter that 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 that is used to set the behavior of the stream.

Return Value: It returns a file pointer resource on success, or FALSE on error.

PHP fopen() Function Exclusion:

  • When writing to a text file, then the correct line-ending character should be used based on the platform. For Instance, Windows systems use \r\n, Unix systems use \n, and Macintosh systems use \r as the line ending character.
  • It is recommended or suggested to use the 'b' flag when opening files with fopen() function.
  • Therefore, an error of level E_WARNING is generated When the open fails.
  • When safe mode is enabled, then PHP checks whether the directory in which the script is operating has the same UID (i.e. owner) as the script that’s being executed.
  • If you are unsure whether the filename is a file or a directory, therefore, you may need to use the is_dir() function before calling the PHP fopen() function since fopen() function may also succeed when the filename is a directory.

php fopen() Function Example

There are the following programs of the fopen() function.

Example:1
Output: This File does not exist!
Example:2
Output: Welcome to Tutorialscan website!
Example:3
Output: Welcome to Tutorialscan website!
Example:4
Output: helloportaltorialscan

Conclusion:

In this article, you have learned PHP fopen() function with different file mode. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.