PHP fopen() Function Tutorial

PHP provides the fopen() function in order to open files. Even fopen() is created to open files it can also open different types of resources and protocols like HTTP, HTTPS, FTP, etc. Opening a file seem simple operation but it may have different file open modes like a read-write, read-only, open file if not exit create, etc. fopen() is derived from the C programming language, and most of the attributes are derived from C.

fopen() Function Syntax

fopen() function has the following syntax. Even there are 4 parameters in general two parameters are used where these are required to call fopen() function.

fopen( $filename , $mode , $use_include_path , $context)
  • $filename is the most important parameter where the file, URL, or similar name and scheme is provided to open. $file parameter is required.
  • $mode parameter is another required parameter where the file or resource open mode is specified. The file can be opened read-only, read/write, etc. File open modes are explained in detail below.
  • $use_include_path is an optional parameter a rarely used.
  • $context is an optional parameter rarely used.

File Open Modes

fopen() function can be used to open files in different modes. The $mode parameter is the second parameter of the fopen() function and required to open a file. Below we will list all possible modes for the fopen() function. The mode parameter is provided as a string in single or double-quotes.

'r'Open for reading only; place the file pointer at the beginning of the file.
'r+'Open for reading and writing; place the file pointer at the beginning of the file.
'w'Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+'Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a'Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
'a+'Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
'x'Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
'x+'Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c'Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it’s desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
'c+'Open the file for reading and writing; otherwise it has the same behavior as 'c'.
PHP fopen() Function File Open Modes

Open File with fopen() Function

We will start with a simple example where we will open file in different ways. We will open file with its absolute path, relative path, Windows file etc.

<?php

   //Open file readonly and place pointer at the beginning of file
   $myfile = fopen("myfile.txt","r");


   //Open file read and write, place pointer at the beginning of file
   $myfile = fopen("myfile.txt","r+");


   //Open file for write-only and create if doesn't exist and overwrite if exist
   $myfile = fopen("myfile.txt","w");


   //Open file and create if doesn't exist and overwrite if exist
   $myfile = fopen("myfile.txt","w+");


   //Open file and create if doesn't exist and overwrite if exist
   $myfile = fopen("/home/ismail/myfile.txt","w+");


   //Open file in Windows
   $myfile = fopen("c://myfile.txt","w+");

?>

Open File Read-Only with fopen() Function

Files can be opened for different operations with diffrent modes. But the read-only mode is one of the most popular mode where the content of the opened files can read but can not be edited. We will explicitly provides examples about opening a file in read-only mode for Linux and Windows operating systems with absolute and relative paths.

<?php

   //Open file readonly and place pointer at the beginning of file
   $myfile = fopen("myfile.txt","r");


   //Open file read and write, place pointer at the beginning of file
   $myfile = fopen("myfile.txt","r");


   //Open file read-only with absolute path
   $myfile = fopen("/home/ismail/myfile.txt","r");


   //Open file in Windows
   $myfile = fopen("c://myfile.txt","r");

?>

Open File and Create If Do Not Exist with fopen() Function

Another popular case with an opening file in PHP is trying to open a file and if the file does not exist create and open a file to write. The w+ mode can be used to open a file for writing and if does not exist create and open for writing.

<?php

   //Open file for writing and create if it does not exist
   $myfile = fopen("myfile.txt","w+");


   //Open file with absolute path for writing and create if it does not exist
   $myfile = fopen("/home/ismail/myfile.txt","w+");


   //Open file in Windows with absolute for writing and create if it does not exist
   $myfile = fopen("c://myfile.txt","w+");

?>

Open Web Page (HTTP) with fopen() Function

Even the fopen() function is created for opening files residing on disks and file system later it gained extra features like opening web pages or HTTP or HTTPS protocol. In order to open a web page the URI scheme should be provided with will be http or https and then the complete or absolute path should be provided. We will use the mode as “r” as we can not change the provided URI or web page.

<?php

   //Open specified HTTP URL or web page
   $myfile = fopen( "http://www.wisetut.com" , "r" );

   //Open specified HTTPS URL or web page
   $myfile = fopen( "https://www.wisetut.com" , "r" );

?>

Open FTP with fopen() Function

FTP or File Transmission Protocol is another popular internet protocol that is used to transfer files over the internet or service via FTP services. The fopen() method can be used to open FTP URI like below. If the remote FTP requires authentication with a username and password we can provide the username and password via the FTP URI. We can use “r” and “w” modes where we can read and write files on the FTP server.

<?php

   //Open specified FTP file to read
   $myfile = fopen( "ftp://www.wisetut.com/file.txt" , "r" );

   //Open specified FTP file to write
   $myfile = fopen( "ftp://www.wisetut.com/file.txt" , "w" );

   //Open specified FTP file with authentication providing username and pass
   $myfile = fopen( "ftp://user:password@www.wisetut.com/file.txt" , "w" );

?>

Open Directory with fopen() Function

Can we open directory with fopen()? Yes, even the fopen() is designed to open files it can also open directories as from the Linux filesystem point of view everything on the system is a file. A directory is a file too and can be opened with the fopen() function.

<?php

   //Open specified Directory or Folder read
   $myfile = fopen( "/home/ismail" , "r" );

   //Open specified Directory or Folder to read
   $myfile = fopen( "ismail" , "r" );

?>

Close Opened File with fclose() Function

Opening files will lock the opened file and take some system resource to work on file. In order to complete opened file operation properly the file should be closed with the fclose() function. Also closing file properly will make changes or any write persistent on the file.

<?php

   //Open specified Directory or Folder read
   $myfile = fopen( "/home/ismail" , "r" );

   //Close file
   flose($myfile);

?>

Leave a Comment