In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to write files in PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
It must not be difficult for everyone to read the file, so let's take a look at how we should modify the file or create the file and modify it.
File_put_contents writes to a file
First, let's learn about a writing method, file_put_contents (), whose syntax format is as follows:
Int file_put_contents (string $file path, string $write data])
It can write a string to the specified file, and if the file does not exist, file_put_contents creates a file that returns the length of the written bytes. At the same time, you need to know that if there is content in this specified file, it will overwrite the previous content.
Examples are as follows:
Output result:
Test.txt does not exist in my directory, but after passing the function, test.txt appears in my directory, and the content is as follows:
From the above example, we can see that it is not difficult to write a file, according to the specified function format, the specified file can be written.
Fwrite cooperates with fopen for write operation
The fwrite () function is used to write a string to the file and returns the number of characters written successfully, otherwise it returns FALSE.
First of all, the grammatical structure of fwrite is as follows:
Int fwrite (resource $file resource variable, string written by string $[, int length])
Next, we use fwrite plus w in fopen to write the file. The feature, as we said in the previous article, is to open it in write mode, point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create it.
Output result:
The test.txt file in the directory contains the following:
From the above example, we can get the following summary:
Whether it is newly created or not, the file will be opened and rewritten.
The contents of the original file will be overwritten.
The file does not exist and will be created
Next, let's compare the differences between several patterns:
R-can only read, not write with fwrite
Renewable music-operable to read and write
W-can only write functions
We can read and write
For example:
In the above example, replace w with r. The example is as follows:
Output result:
Through the example, we find that the write in r mode is not successful.
The difference between a mode and w mode
Try replacing r with a through the above code. The example is as follows:
Output result:
The test.txt text is as follows:
Through the above example, it is found that in mode a, each refresh will be written one more time. Each write appends content to the end of the file.
Thank you for reading! This is the end of the article on "how to write a file in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.