Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

Writing or Appending to a File

The processes for writing to or appending to a file are the same. The difference lies in the fopen() call. When you write to a file, you should use the mode argument "w" when you call fopen():

$fp = fopen( "test.txt", "w" );

All subsequent writing will occur from the start of the file. If the file doesn't already exist, it will be created. If the file already exists, any prior content will be destroyed and replaced by the data you write.

When you append to a file, you should use mode "a" in your fopen() call:

$fp = fopen( "test.txt", "a" );

Any subsequent writes to your file are added to the existing content.

Writing to a File with fwrite() or fputs()

fwrite() accepts a file resource and a string. It then writes the string to the file. fputs() works in exactly the same way.

fwrite( $fp, "hello world" );
fputs( $fp, "hello world" );

Writing to files is as straightforward as that. Listing 10.13 uses fwrite() to print to a file. We then append a further string to the same file using fputs().

Listing 10.13 Writing and Appending to a File

 1: <html>
 2: <head>
 3: <title>Listing 10.13 Writing and appending to a file</title>
 4: </head>
 5: <body>
 6: <?php
 7: $filename = "test.txt";
 8: print "Writing to $filename<br>";
 9: $fp = fopen( $filename, "w" ) or die("Couldn't open $filename");
10: fwrite( $fp, "Hello world\n" );
11: fclose( $fp );
12: print "Appending to $filename<br>";
13: $fp = fopen( $filename, "a" ) or die("Couldn't open $filename");
14: fputs( $fp, "And another thing\n" );
15: fclose( $fp );
16: ?>
17: </body>
18: </html>

Locking Files with flock()

The techniques you have learned for reading and amending files will work fine if you are only presenting your script to a single user. In the real world, however, you would expect many users to access your projects more or less at the same time. Imagine what would happen if two users were to execute a script that writes to one file at the same moment. The file will quickly become corrupt.

PHP 4 provides the flock() function to forestall this eventuality. flock() will lock a file to warn other process against writing to or reading from a file while the current process is working with it. flock() requires a valid file resource, and an integer representing the kind of lock you would like to set. PHP 4 provides predefined constants for each of the integers you are likely to need. In Table 10.1 we list three kinds of locks you can apply to a file.

Table 10.1 Integer arguments to the flock() function



Lock type





Allows other processes to read the file but prevents writing (used when reading a file)




Prevents other processes from either reading from or writing to a file (used when writing to a file)




Releases a shared or exclusive lock

You should call flock() directly after calling fopen() and then call it again to release the lock before closing the file.

$fp = fopen( "test.txt", "a" ) or die("couldn't open");
flock( $fp, LOCK_EX ); // exclusive lock
// write to the file
flock( $fp, LOCK_UN ); // release the lock
fclose( $fp );


Locking with flock() is advisory. Only other scripts that use flock() will respect a lock that you set.

  • + Share This
  • 🔖 Save To Your Account