fopen PHP Function | How do I open a PHP file with example?

Summary: in this article, you will learn about fopen PHP Function with example and how do I open a PHP file? Let’s understand the fopen() PHP Function with an example.

Definition and Usage

File handling is the most important concept in the world of computer science technology. Files are a type of portable storage that is used to store any type of data. furthermore, before a programmer applies any algorithm on a particular file, they need to open that file using the fopen() PHP command.

The fopen PHP function opens a file or URL. If the function fails, when it returns FALSE and an error on failure. Therefore, you can add an '@' in front of the function name to hide the error output.

Syntax

Only two parameters are passed into the PHP fopen() function:

  • Name of the file
  • File permissions
$file = fopen("filename.txt", "r");

File Permissions

  • r = it reads a file if they exist.
  • w = Writes in an existing file.
  • r+ = Reads a file for input and output; the file must exist.
  • w+ = Creates files and opens them for both input and output.

Technical Details

Return Value: TRUE on success, FALSE, and an error on failure. you’ll hide the error by adding "@" in front or ahead of the function name.
PHP Changelog: PHP 7.1: Added "e" option
Version: 4.3+
PHP 5.2: Added "c" and "c+" options

fopen PHP Example

The given below code segment opens a file and writes “Welcome to TutorialScan!” in it.

fopen PHP Related Question and Answer:

Question1: How do I open a php file?

Answer: fopen() function in PHP is used to open files or URLs and returns resources. The PHP fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opened and $mode represents the file mode for example read-only, read-write, write-only, etc.
There are the following mode which is used to open a PHP file, see the given below table.

Mode Description
r Opens the file in read-only mode. It places the file pointer at the starting or beginning of the file
r+ Opens file in read-write mode. It places the file pointer at the starting or beginning of the file.
w Opens the file in write-only mode. It places the file pointer to the starting or beginning of the file and truncates the file to zero length. If the file isn’t found, furthermore, it creates a new file.
w+ Opens file in read-write mode. It places the file pointer to the starting or beginning of the file and truncates the file to zero length. If the file isn’t found, it creates a new file.
a Opens a file in write-only mode. this file mode places the file pointer to the end of the file. If the file isn’t found, furthermore, then it creates a new file.
a+ Opens a file in read-write mode. this file mode places the file pointer to the end of the file. If the file isn’t found, then it creates a new file.
c Opens the file in write-only mode. If the file doesn’t exist, it’s created. furthermore, If it exists, it is neither truncated (as opposed to ‘w’), nor the call to this function fails (as is the case with ‘x’). The file pointer is positioned at the starting or beginning of the file
c+ It is that the same as c however it opens the file in read-write mode.
x Creates and opens files in write-only mode. It places the file pointer at the starting or beginning of the file. furthermore, If file is found, PHP fopen() function returns FALSE.
x+ It is that the same as x however it creates and opens files in read-write mode

Question2: Does Fopen create file?

Answer: Use the PHP number_format() function. This will show or display exactly two digits after the decimal point. Advantage: If you wish to display two digits after a float value only and not for int, then use this


Question3: What does Fopen do in PHP?

Answer: The PHP fopen() function opens a file or URL. If the function fails, then it returns FALSE and an error on failure.

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.