File File Opening-fopen() Function in PHP With Example

File Opening-fopen():

The PHP fopen() function is used to open a file.


Syntax:




Here,

  • "fopen": is the PHP open file function
  • "$file_name": is the name of the file to be opened
  • "$mode": is the mode in which the file should be opened.
ModeDescription

r

Read only mode, file pointer will set at the starting position of file.

r+

Read and write mode, file pointer will set at the starting of file.

w

Write mode only, if the file exists it will open otherwise a new file will be created. If the file already exists, earlier contents will be overwritten

w+

Write and read mode, if file exists contents will be overwritten otherwise a new file will be created.

a

Append file, if file exists it will open and file pointer will set at the ending of file, if it doesn’t exists a new file will be created.

a+

Read and append mode, if file exists the pointer will set at the end of file, otherwise a new file will be created.

x

It is used to create and open a file only for writing purpose, if file exists this mode will return false and display an error. If file doesn’t exists it will simply create a file


Example:


Read Also: