Section 13.2. Creating and Changing Files


13.2. Creating and Changing Files

Like reading files, creating and changing files can also be done in more than one way. There are just two options this time: file_put_contents( ) and fwrite( ). Both of these functions complement functions we just looked at, which are file_get_contents( ) and fread( ), respectively, and they mostly work in the same way.

13.2.1. file_put_contents( )

This function writes to a file with the equivalent of fopen( ), fwrite( ) (the opposite of fread( )), and fclose( )all in one function, just like file_get_contents( ). It takes two parameters: the filename to write to and the content to write, respectively, with a third optional parameter specifying extra flags that we will get to in a moment. If file_put_contents( ) is successful, it will return the number of bytes written to the file; otherwise, it will return false.

Here is an example:

     $myarray[ ] = "This is line one";     $myarray[ ] = "This is line two";     $myarray[ ] = "This is line three";     $mystring = implode("\n", $myarray);     $numbytes = file_put_contents($filename, $mystring);     print "$numbytes bytes written\n"; 

That should output "52 bytes written", which is the sum total of the three lines of text plus the two new line characters used to implode( ) the array. Remember that the new line character is, in fact, just one character inside files, whereas PHP represents it using two: \ and n.

You can pass in a third parameter to file_put_contents( ) which, if set to FILE_APPEND, will append the text in your second parameter to the existing text in the file. If you do not use FILE_APPEND, the existing text will be wiped and replaced.

13.2.2. fwrite( )

The opposite of fread( ) is fwrite( ), which also works with the file handle returned by fopen( ). This takes a string to write as a second parameter, and an optional third parameter where you can specify how many bytes to write. If you do not specify the third parameter, all of the second parameter is written out to the file.

As with fread( ), PHP will stop writing when it reaches the end of the string or when it has reached the number of bytes specified in this length parameter, whichever comes firstyou don't need to worry about specifying more bytes than you have in the string.


Here is an example using the variable $mystring from the previous example to save space:

     $handle = fopen($filename, "wb");     $numbytes = fwrite($handle, $mystring);     fclose($handle);     print "$numbytes bytes written\n"; 

If I had added 10 as the third parameter to the fwrite( ) call, only the first 10 bytes of $mystring would have been written out. Note again that fclose( ) is called immediately after the file handle is finished with, which is always the best practice.

The fwrite( ) function uses a file pointer in the same way as fread( ). As you write out data, PHP moves the file pointer forward so that you always write to the end of a file (unless you move the file pointer yourself).



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

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