# PHP Open File

PHP Open File

To open a file in PHP, fopen() function is used. In general, fopen() function accepts two parameters:

Filename:  This parameter specifies the name of the file to be opened.

Mode: This parameter specifies the mode in which the file should be opened.

Syntax:

resource fopen ( string $filename , string$mode [, bool $use_include_path = false [, resource$context ]] )

OR

resource fopen ( string $filename , string$mode )

MODES of fopen function:

 \$mode MODE DESCRIPTION r Read only mode Pointer starts from the beginning of the file. w Write only mode Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file. a Write only mode Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file. x Write only mode Creates a new file if it doesn’t exist or returns FALSE if the file already exists. Pointer starts from the beginning of the file. c Write only mode Creates a new file if it doesn’t exist or does nothing if the file already exists. Pointer starts from the beginning of the file. r+ Read Write mode Pointer starts from the beginning of the file. w+ Read Write mode Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file. a+ Read Write mode Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file. x+ Read Write mode Creates a new file if it doesn’t exist or returns FALSE if the file already exists. Pointer starts from the beginning of the file c+ Read Write mode Creates a new file if it doesn’t exist or does nothing if the file already exists. Pointer starts from the beginning of the file.