Hudzilla.org - the homepage of Paul Hudson
Contents > Files > Creating and changing files Wish List | Report Bug | About Me ]

8.2.2     fwrite()

This is NOT the latest copy of this book; click here for the latest version.

int fwrite ( resource handle, string string [, int length])

The opposite to fread() is fwrite() and also works with the file handle returned by fopen(). Fwrite() 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, which is often what you want.

Author's Note: 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 first - you do not 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:

<?php
    $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 ten bytes of $mystring would have been written out. Note again that fclose() is called immediately after it was finished with, which is always best practice.

Fwrite() uses a file pointer in the same way as fread() - as you write bytes out, PHP advances the file pointer appropriately, meaning that, unless you specifically move the file pointer yourself, you always write to the end of a file.





<< 8.2.1 file_put_contents()   8.3 Moving, copying, and deleting files: rename(), copy(), and unlink() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Elite Charizard - 07 Sep 2008

Did you chmod phptest.txt to 777?

A PHP User - 07 Sep 2008

THIS DIDN'T WORK FOR ME. THIS IS WHAT I GOT WHEN I TRIED IT.

Warning: fopen(c:\phptest.txt) [function.fopen]: failed to open stream: Permission denied in c:\inetpub\wwwroot\creative.php on line 78

Warning: fwrite(): supplied argument is not a valid stream resource in c:\inetpub\wwwroot\creative.php on line 79

Warning: fclose(): supplied argument is not a valid stream resource in c:\inetpub\wwwroot\creative.php on line 80



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow