English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Filesystem Reference Manual
The fwrite() function can write to an open file. This function can stop at the end of the file or at a specified length, whichever comes first. This function can return the number of bytes written, or false if it fails.
int fwrite ( resource $handle , string $string [, int $length ] )
Write the content of the string to the file pointer handle.
If length is specified, writing will stop when length bytes have been written or the end of the string is reached, whichever comes first.
Note that if the length parameter is given, the magic_quotes_runtime configuration option will be ignored, and the backslashes in the string will not be stripped.
<?php $file = fopen("/PhpProject/sample.txt", "w"); echo fwrite($file, "Hello w3codebox!!!!!"); fclose($file); ?>
Output Result
25
<?php $filename = ""/PhpProject/sample.txt" $somecontent = "This content is added to the file\n"; if(is_writable($filename)) { if(!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } if(fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, the content ($somecontent) has been written to the file ($filename)"; fclose($handle); } else { echo "File ($filename) cannot be written"; } ?>
Output Result
Success, the content (this content) has been written to the file (/PhpProject/sample.txt)