Writing or Appending to a File

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

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

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

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

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

Any subsequent writes to your file are added to the existing content, but if you attempt to append content to a nonexistent file, the file is first created.

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

fwrite() accepts a file resource and a string, and 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> 

The screen output of this script, when run from your Web browser, is

 Writing to test.txt Appending to test.txt 

If you open the test.txt file or use listing10.9.php to read its contents, you'll find the file now contains:

 Hello world And another thing 

Locking Files with flock()

The techniques you learned for reading and amending files work fine if you're presenting your script to only a single user. In the real world, however, you'd expect many users to access your projects at more or less 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 would quickly become corrupt.

PHP provides the flock() function to forestall this eventuality. flock() locks a file to warn other processes against writing to or reading from that file while the current process is working with it. flock() requires a valid file resource and an integer representing the kind of lock you want to set. PHP provides predefined constants for each of the integers you're likely to need. Table 10.1 lists three kinds of locks you can apply to a file.

Table 10.1. Integer Arguments to the flock() Function

Constant

Integer

Lock Type

Description

LOCK_SH

1

Shared

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

LOCK_EX

2

Exclusive

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

LOCK_UN

3

Release

Releases a shared or exclusive lock

You should call flock() directly after calling fopen() and call it again to release the lock before closing the file. If the lock is not released, you will not be able to read from or write to 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); 

graphics/bulb.gif

For more information on file locking, see the PHP Manual entry for the flock() function, at http://www.php.net/manual/en/function.flock.php.




Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net