Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Open File



When working with PHP, you will need to read and write files. This chapter explains how to open a file in PHP. This is a important skill for any web developer who want to manage data properly.

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.

Why Do We Need to Open Files?

Opening files allows you to −

  • Retrieve data (like user submissions).

  • Save data (like user-generated content).

  • Handle various types of file formats (like JSON, CSV, and XML).

How to Open a File in PHP

To open a file with PHP, use the fopen() function. This function needs two parameters −

  • The name of the file.

  • The mode in which you want to open the file.

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

Parameters

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
   // Opening a file for writing   
   $handle = fopen("hello.txt", "w");

   $handle = fopen("c:/xampp/htdocs/welcome.png", "rb");

   // Opening a file for reading
   $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);

Important Notes

Here are some important point you need to keep in mind while working with file Handling −

  • Always make sure the file opened successfully. It is a good idea to verify that the file opened successfully. If it did not, you can accept the error carefully.

  • When you are finished reading or writing, always use fclose() to close your file. This releases system resources.

Advertisements