PHP | fopen( ) (Function open file or URL)

 The fopen() function in PHP is used to open a file or URL and returns a file pointer resource that can be used to read, write or manipulate the contents of the file.

Here's the basic syntax for using the fopen() function:

$file_pointer = fopen($filename, $mode);


  • $filename is the name of the file or URL to be opened.
  • $mode specifies the mode in which the file should be opened, such as "r" for read-only, "w" for write-only, "a" for append, etc.

Here are some examples of opening a file using fopen():

// Open a file for reading $file = fopen("example.txt", "r"); // Open a file for writing $file = fopen("example.txt", "w"); // Open a file for appending $file = fopen("example.txt", "a");

Once the file is opened, you can perform various operations on it, such as reading from it, writing to it, or closing it using the fclose() function.

It's important to note that the fopen() function returns a file pointer resource, which is a special data type in PHP used to represent a file that has been opened. This file pointer resource must be passed to other file functions in order to operate on the file.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755