PHP | fopen( ) (original) (raw)

Last Updated : 05 Apr, 2025

The fopen() function in PHP is used to open a file or URL. It returns a file pointer that can be used with other functions like fread(), fwrite(), and fclose(). The function allows developers to interact with files in various ways, including reading, writing, and appending data.

**Syntax:

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

**In this syntax:

Implementation of fopen()

1. Opening a File for Reading

Here’s an example of how to open a file in read-only mode ('r'):

php `

`

**Output:

Screenshot-2025-04-05-115952

fopen()

**In this example:

**2. Writing to a File

Here’s an example of opening a file in write-only mode ('w') and writing some data to it:

php `

`

**Output:

Screenshot-2025-04-05-120338

Writing in a file

**In this example:

**3. Appending Data to a File

If you want to append data to an existing file, you can open it in append mode ('a'):

php `

`

**Output:

Screenshot-2025-04-05-120713

Appending Data to a File

**In this example:

Common Issues with fopen()