• PHP Video Tutorials

PHP - Open File



PHP’s built-in function library provides fopen() function to open a file or any other stream and returns its "reference pointer", also called as "handle".

The fopen() function in PHP is similar to fopen() in C, except that in C, it cannot open a URL.

Syntax of fopen()

The fopen() function has the following signature −

fopen(
   string $filename,
   string $mode,
   bool $use_include_path = false,
   ?resource $context = null
): resource|false

The $filename and $mode parameters are mandatory. Here’s the explanation of the parameters −

  • $filename − This parameter is a string representing the resource to be opened. It may be a file in the local filesystem, or on a remote server with the scheme:// prefix.

  • $mode − A string that represents the type of access given to the file/resource.

  • $use_include_path − A Boolean optional parameter can be set to '1' or true if you want to search for the file in the include_path, too.

  • $context − A context stream resource.

Modes of Opening a File

PHP allows a file to be opened in the following modes −

Modes Description
r Open a file for read only.
w Open a file for write only. creates a new file even if it exists.
a Open a file in append mode
x Creates a new file for write only.
r+ Open a file for read/write.
w+ Open a file for read/write. creates a new file even if it exists.
a+ Open a file for read/write in append mode.
x+ Creates a new file for read/write.
c Open the file for writing, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode).
c++ Open the file for read/write, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode).
e Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.

If the fopen() function is successfully executed, it returns a "file pointer" or "handle" resource bound to the file stream. However, if it fails, it returns false with E_WARNING being emitted.

$handle = fopen('a.txt, 'r');
var_dump($handle);

If the file exists in the current directory, the success is shown by the output

resource(5) of type (stream)

If not, you get the following error message

Warning: fopen(a.txt): Failed to open stream: 
No such file or directory in a.php on line 2
bool(false)

Examples

The following examples show different usages of the fopen() function −

<?php
   $handle = fopen("hello.txt", "w");
   $handle = fopen("c:/xampp/htdocs/welcome.png", "rb");
   $handle = fopen("http://localhost/hello.txt", "r");
?>

Note that this function may also succeed when the filename is a directory. In that case, you may need to use the is_dir() function to check whether it is a file before doing any read/write operations.

Once a file is opened, you can write data in it with the help of functions such as fwrite() or fputs(), and read data from it with fread() and fgets() functions.

Closing a File

It is always recommended to close the open stream referenced by the handle −

fclose($handle);
Advertisements