Phpfile_put_contents () function (integrated with fopen, fwrite, and fclose)

Source: Internet
Author: User
Phpfile_put_contents () function (integrated with fopen, fwrite, and fclose). For more information, see. Command: file_put_contents ();
Command parsing: file_put_contents (PHP 5)
File_put_contents -- write a string to a file
Note:
Int file_put_contents (string filename, string data [, int flags [, resource context])

It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.
The parameter data can be an array (but not a multi-dimensional array), which is equivalent to file_put_contents ($ filename, join ('', $ array ))
Since PHP 5.1.0, the data parameter can also be specified as a stream resource. the cached data stored in stream will be written to the specified file. this usage is similar to stream_copy_to_stream () function.
Parameters

Filename
The name of the file to be written.
Data
The data to be written. The type can be string, array, or stream resources (as mentioned above ).
Flags
Flags can be FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX (get an exclusive lock). However, be cautious when using FILE_USE_INCLUDE_PATH.
Context
A context resource.
Write code (the code itself is error-free, but has learned another feature ):

The code is as follows:


$ Contents = "this is the content written using file_put_contents ";
$ Contents2 = array ("This is used", "file_put_contents", "Command written content ");
File_put_contents ("html/caceh.txt", $ contents );
File_put_contents ("html/cache2.txt", $ contents2 );
?>


Code Analysis: computation uses the file_put_contentscommand to write strings to cache.txt and cache2.txt files.
Result: the caceh.txt file is added to the HTML directory ----
Note: The file_put_contents () function integrates the fopen (), fwrite (), and fclose () functions. the new file in this example is the function of fopen.

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.