How to Open a File in PHP

By eHow Internet Editor

Rate: (2 Ratings)

Reading from and writing to files is one of the most common programming tasks. You can't process data if you don't get it from somewhere (most often a file), and you will usually need to store the result of the processing in something different from the volatile RAM. Again, one of the most common solutions is a file, in which you write the result of the processing. Of course, if you want to be able to read and write to files, you need to know how to open them.

Instructions

Difficulty: Challenging

Things You’ll Need:

  • PHP 5, installed and properly configured
  • PHP IDE, or at least a text editor
  • Web server--preferably Apache
  • MySQL database server

Open a File in PHP as Read-Only

Step1
Use the 'r' option to open a file as read-only. When a file is opened as read-only, the file pointer is placed in the beginning of the file and you start reading from there.
$myFile = "simpleFile.txt";
$fh = fopen($myFile, 'r') or die("Can't open file");
fclose($fh);
Step2
Use the more advanced way, with the 'r+' option, to open a file for reading. The 'r+' option opens a file for both reading from and writing to. As with the 'r' option, the file pointer is at the beginning of the file.
$myFile = "simpleFile.txt";
$fh = fopen($myFile, 'r+') or die("Can't open file");
fclose($fh);

Open a File as Write-Only

Step1
Use the 'w' option, to open a file as write-only. It is important to note that this option erases the contents of the file, places the pointer in the beginning of the file and starts writing from there.
$myFile = "simpleFile.txt";<br>$fh = fopen($myFile, 'w') or die("Can't open file");<br>fclose($fh);
Step2
Use the more advanced way, the 'w+' option, to open a file for writing. The 'w+' option opens a file for both reading from and writing to. The difference with 'r+' is that the 'w+' option deletes all information in the file when the file is opened.
$myFile = "simpleFile.txt";<br>$fh = fopen($myFile, 'w+') or die("Can't open file");<br>fclose($fh);

Open a File for Appending

Step1
Use the 'a' option to open the file for writing. The data in the file is not deleted, and the file pointer is placed at the end of the file so that you start writing the new contents after the existing data.
$myFile = "simpleFile.txt";
$fh = fopen($myFile, 'a') or die("Can't open file");
fclose($fh);
Step2
Use the more advanced 'a+' option to open the file for appending. The difference with 'r+' is that the file pointer is positioned at the end of the file.
$myFile = "simpleFile.txt";
$fh = fopen($myFile, 'a+') or die("Can't open file");
fclose($fh);

Tips & Warnings

  • For high-level access, you can use the functions file_get_contents and file_put_contents. These functions allow you to read the contents of the file and to write into it, respectively.
  • Master the basic ways of opening a file first, and then move on to the '+' options.
  • When you are done with reading/writing the file, close it with the fclose function.
  • Be careful how you open a file. If you open the file for writing (rather than for appending), it will delete the existing content of the file--so think twice before you open a file for writing.
  • Create a backup copy of the files you plan to open. If you accidentally make a mistake and delete the contents of the file and you don't have a backup copy, the damage will be beyond repair.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article: How to Open a File in PHP

eHow Internet Editor

eHow Internet Editor

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.