PHP function fwrite secure for binary files

Source: Internet
Author: User

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

 
 
  1. <? Php
  2. $ Filename = 'test.txt ';
  3. $ Somecontent = "add these words to file n ";
  4. // First, make sure that the file exists and is writable.
  5. If (is_writable ($ filename )){
  6. // In this example, we will use the Add Mode
    Open $ filename,
  7. // Therefore, the file pointer will start with the file,
  8. // That is, when we use fwrite,
    $ Somecontent: the place to be written.
  9. If (! $ Handle = fopen ($ filename, 'A ')){
  10. Echo "the file $ filename cannot be opened ";
  11. Exit;
  12. }
  13. // Write $ somecontent to the open file.
  14. If (fwrite ($ handle, $ somecontent)
    === FALSE ){
  15. Echo "cannot be written to file $ filename ";
  16. Exit;
  17. }
  18. Echo "successfully writes $ somecontent
    To File $ filename ";
  19. Fclose ($ handle );
  20. } Else {
  21. Echo "File $ filename cannot be written ";
  22. }
  23. ?>

The above describes how to use the PHP function fwrite.


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.