Usage of PHP function fwrite

Source: Internet
Author: User
PHP function fwrite--write file (can be used safely for binary files)

Description

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

The PHP function fwrite writes the contents of a string to the file pointer handle. If length is specified, the write stops when the length byte is written or after the string is written, depending on which situation is encountered first.

V returns the number of characters written, or False if an error occurs.

Note If the length parameter is given, the magic_quotes_runtime configuration option is ignored, and the slash in the string will not be drawn.

Note: When opening a file on a system that distinguishes between binary and text files (such as Windows), the mode parameter of the fopen () function is added ' B '.

Example 1. A simple PHP function fwrite example

< php  $filename = ' test.txt ';  $somecontent = "Add these words to file \ n";  First we want to make sure that the file exists and is writable.  if (is_writable ($filename)) {  //In this example, we will use Add mode to open $filename,  //So the file pointer will be at the beginning of the file,/  / That is where $somecontent will write when we use Fwrite ().  if (! $handle = fopen ($filename, ' a ')) {   echo "Cannot open file $filename";  Exit;  }  Write the $somecontent to the file we opened.  if (fwrite ($handle, $somecontent) = = = FALSE) {  echo "cannot write to file $filename";  Exit;  }  echo "successfully wrote $somecontent to file $filename";  Fclose ($handle);  } else {  echo "file $filename not writable";  }  ? >

The above is the PHP function Fwrite related use method.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.