Its prototype follows:
int fwrite(resource handle, string string [, int length])
298 CHAPTER 10 ?– WORKING WITH THE FI LE AND OPERATING SYSTEM
If the optional length parameter is provided, fwrite() will stop writing when
length characters have been written. Otherwise, writing will stop when the end of the
string is found. Consider this example:
// Data we'd like to write to the subscribers.txt file
$subscriberInfo = "Jason Gilmore|jason@example.com";
// Open subscribers.txt for writing
$fh = fopen("/home/www/data/subscribers.txt", "at");
// Write the data
fwrite($fh, $subscriberInfo);
// Close the handle
fclose($fh);
?>
?– Tip If the optional length parameter is not supplied to fwrite(), the magic_quotes_runtime
configuration parameter will be disregarded. See Chapters 2 and 9 for more information about this
parameter. This only applies to PHP 5 and earlier.
Moving the File Pointer
It??™s often useful to jump around within a file, reading from and writing to various
locations. Several PHP functions are available for doing just this.
Moving the File Pointer to a Specific Offset
The fseek() function moves the pointer to the location specified by a provided offset
value. Its prototype follows:
int fseek(resource handle, int offset [, int whence])
If the optional parameter whence is omitted, the position is set offset bytes from
the beginning of the file.
Pages:
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372