• Tutorial List

Open Files using PHP fopen() function

Last updated Jul 08, 2020

How to open a file using PHP?

You must open that file before reading and writing or creating a file. To open a file using PHP, you have to use the PHP fopen() function.

Syntax of the fopen()

<?php
  	// File Name
  	$filename = 'c:\folder\resource.txt';

	// Mode (r = 'read mode')
	$mode = "r";

	// Optional Parameter
	$use_include_path = true;
 	$myFile = fopen($filename,$mode);
?>
  • File Name
    • Enter the file name with the path, which you want to open.
    • If your .php file and the .txt file are in the same folder, then you don’t have to enter the full path, just enter the filename.
  • Mode
    • The mode defines, what is the purpose of opening a file. purpose means – you have to read or write or do both with a file.
  • use_include_path
    • It is an optional parameter can be set to ‘1’ or TRUE if you want to search for the file in the include_path, too.

📝 The fopen() function will return an error if the file does not exist which you want to open, or if the file does not have the read or write permission.


Example of the fopen() function

Not only the .txt file, You can open every type of file using the fopen() function. Such as image file (.png, .gif), zip file (.zip) etc.

<?php
  // OPENING A FILE (WITH THE READ MODE)
  $myFile = fopen('c:\folder\resource.txt','r');

  // OPENING A URL (WITH THE READ MODE)
  $myURL = fopen("http://example.com/",'r');

  // OPENING A IMAGE FILE
  $myImage = fopen('c:\folder\my.png', 'r');

  //OPENING A FILE THROUGH FTP URL
  $myFTPFile = fopen("ftp://user:[email protected]/file.txt",'r');
?>

List of modes for fopen() function

ModeDescription
'r'Open for reading only. The file pointer starts at the beginning of the file.
'r+'Open for reading and writing. The File pointer starts at the beginning of the file.
'w'Open for writing only. The file pointer starts at the beginning of the file and Erases the existing contents of the file. If the file does not exist, it will create it.
'w+'It works the same as 'w' mode, but it Open for reading and writing.
'a'Open for writing only. The file pointer starts at the end of the file. If the file does not exist, it will create it. fseek() has no effect, writes are always appended.
'a+'It works the same as 'a' mode, but it Open for reading and writing, & fseek() only affects the reading position.
'x'It will create a new file, and open that file for writing only. if the file already exists, it will return FALSE and an error.
'x+'It has the same behavior as 'x'. But it open for reading and writing.

Important Notes

The different operating system has different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) according to your operating system –

  • Unix based systems – \n
  • Windows-based systems – \r\n
  • Macintosh-based systems – \r

Windows offers a text-mode translation flag 't' that will transparently translate \n to \r\n when working with the file. In contrast, you can also use ‘b’ to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

You should use the ‘t’ mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the ‘b’ in all other cases (Especially working with binary files).

Example:

<?php
	$myFile = fopen('c:\folder\my.png', 'rb');
	$myFile2 = fopen('my-file.txt', 'rt');
?>

How to stop your PHP script after failed to open files?

If you want to stop your PHP script after failed to open files, you have to use the PHP die() function.

<?php
	$myNewFile = fopen('new-file2.txt','r') or die("Oops, Failed to open the file.");
?>

Ask Questions

Your email address will not be published. Required fields are marked *