PHP function fwrite-write a file (which can be safely used for binary files)
Description
Int fwrite (resource handle, string [, int length])
The PHP function fwrite writes the string content to the handle of the file pointer. If length is specified, the write will stop after the length byte is written or the string is written.
V returns the number of written characters. If an error occurs, FALSE is returned.
Note that if the length parameter is provided, the magic_quotes_runtime configuration option will be ignored, and the diagonal lines in the string will not be extracted.
Note: 'B' must be added to the mode parameter of the fopen () function when files are opened on a system that distinguishes binary files from text files (such as Windows '.
Example 1. A simple PHP function fwrite example
- <? Php
- $ Filename = 'test.txt ';
- $ Somecontent = "add these words to file n ";
- // First, make sure that the file exists and is writable.
- If (is_writable ($ filename )){
- // In this example, we will use the Add Mode
Open $ filename,
- // Therefore, the file pointer will start with the file,
- // That is, when we use fwrite,
$ Somecontent: the place to be written.
- If (! $ Handle = fopen ($ filename, 'A ')){
- Echo "the file $ filename cannot be opened ";
- Exit;
- }
- // Write $ somecontent to the open file.
- If (fwrite ($ handle, $ somecontent)
=== FALSE ){
- Echo "cannot be written to file $ filename ";
- Exit;
- }
- Echo "successfully writes $ somecontent
To File $ filename ";
- Fclose ($ handle );
- } Else {
- Echo "File $ filename cannot be written ";
- }
- ?>
The above describes how to use the PHP function fwrite.