reading and writing files

There are higher level file reading and writing PHP functions. However, if you use the basic PHP functions for reading and writing to files then you’ll be taking the following steps:

  1. Open stream to file
  2. Read file into string
  3. Possibly close stream
  4. Edit string
  5. Possibly open stream to file
  6. Write string to file
  7. Close stream to file

Some subtle issues to consider:

  • The type of read or write stream you opened.
  • Whether to close the stream after reading then re-open to write; Or, keep it open until after you write.

Open stream to file

I will use the the term stream to mean an opened file connection as when you use fopen().

fopen() returns a file pointer resource (file descriptor) on success, or FALSE on error. Assign the return value to a variable because you will need it later.

It is good to close the stream as soon as it is no longer needed because PHP limits the number of streams which can be open at any given time. However, if you fail to close a file descriptor, PHP will do it for you when the script ends.

When you open a stream you must specify the mode in which it is to be opened. The mode in which a file is opened affects the behavior of file writes and file reads. Make sure you have a thorough understanding of the effect of the mode you choose to use. Otherwise, you may end up corrupting or deleting the file you are working on. See the PHP manual for fopen().

Review the manual for fopen() before using it. There are many options. For example you can specify whether you want the stream open in binary or text mode.

There are four main types of file connections that can be opened: HTTP, FTP, standard I/O, and filesystem. I will concern myself with only one type of file connection: filesystem. The default type of connection is filesystem.

$fCon = fopen("/srl/somedir/somefile.txt", "r");

Notice fopen() can easily open files above the webroot directory. Also, the file being opened does not get interpreted by PHP/web server; It gets read as raw text (even if you name it with a .php extension.)

Read file into string


$fStr = fread($fCon, filesize("/srl/somedir/somefile.txt"));

Beginning with PHP4.3.0, a new function called file_get_contents() was made available. This function returns the entire contents of a file as a string, including the fopen().

Write string to file


$fCon = fopen("/srl/somedir/somefile.txt", "w");
$fWrite = fwrite($fCon, $fStr);
if ($fWrite != strlen($fStr)){
  echo "file write failed!";

The function fwrite() returns the number of characters written.

In PHP there is a mechanism to disable the capability to file-write. This is recommended if your site is entirely database-driven, in which case you don’t have any need to write to the filesystem with PHP. To disable file writing, add fwrite to the list of disabled functions in php.ini.


About samehramzylabib

See About on
Aside | This entry was posted in Coding, PHP and Filesystem and tagged , , . Bookmark the permalink.


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s