Home  >  Article  >  Backend Development  >  php file_put_contents function (integrated fopen, fwrite, fclose)

php file_put_contents function (integrated fopen, fwrite, fclose)

WBOY
WBOYOriginal
2016-07-29 08:45:141612browse

Command: file_put_contents();
Command analysis: file_put_contents (PHP 5)
file_put_contents -- Write a string to a file
Instructions:
int file_put_contents ( string filename, string data [, int flags [, resource context]] )
It has the same function as calling fopen(), fwrite() and fclose() in sequence.
The data parameter 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 saved in the stream will be written to the specified file. This usage is similar to using the stream_copy_to_stream() function.
Parameter
filename
The name of the file to which data will be written.
data
Data to be written. The type can be string, array or stream resource (as mentioned above).
flags
flags can be FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX (to obtain an exclusive lock), however use FILE_USE_INCLUDE_PATH with extreme caution.
context
A context resource.
Write the code (the code itself is correct, but I learned another function of it by mistake):

Copy the code The code is as follows:


$contents = "This is using The content written by file_put_contents";
$contents2 = array("This is the content written using ","file_put_contents","The content written by the command");
file_put_contents("html/caceh.txt",$contents);
file_put_contents( "html/cache2.txt",$contents2);
?>


Code analysis: Plan to use the file_put_contents command to write strings to the two files cache.txt and cache2.txt.
Result: The caceh.txt file has been added to the html file directory. Do you understand?——
Remember: the file_put_contents() function integrates three functions: fopen(), fwrite(), and fclose(). The newly created file in the example is the function of fopen().

The above has introduced the php file_put_contents function (integrated fopen, fwrite, fclose), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn