php creates and modifies file content
In the previous section we learned that reading files is particularly easy to master. In this section we will explain writing to files.
file_put_contentsWrite file
##Let’s first learn the first way to write a file:
int file_put_contents (string $file path, string $write data])
Function: Write a string to the specified file, and create the file if it does not exist. What is returned is the length of written bytes<?php $data = "在PHP中文网学好PHP,妹子票子不再话下!"; $numbytes = file_put_contents('binggege.txt', $data); if($numbytes){ echo '写入成功,我们读取看看结果试试:'; echo file_get_contents('binggege.txt'); }else{ echo '写入失败或者没有权限,注意检查'; } ?>We found that writing files is quite simple. According to the format of this function, specify the file and write the string data.
fwrite cooperates with fopen to perform write operations
##int fwrite (resource $file resource variable, string $written string [, int length])
Note: The alias function of fwrite is fputs
We tried r mode in the last class, which was only used when reading. Next, we use fwrite plus w in fopen to write files in write mode.
Let’s take a look at the features:
Open the writing mode, point the file pointer to the file header and cut the file size to zero. If the file does not exist then attempts to create it.Note: In the following experiment, you can try to create a new test.txt file and write content into it. Then, you can try to delete test.txt. See what tips there are.
<?php $filename = 'test.txt'; $fp= fopen($filename, "w"); $len = fwrite($fp, '我是一只来自北方的狼,却在南方冻成了狗'); fclose($fp); print $len .'字节被写入了\n'; ?>
Summary:
1. Regardless of whether there is a new file, the file will be opened and rewritten2. The original file content will be overwritten
3. If the file does not exist, it will be created
Let’s compare the differences between the following modes:
Description | |
---|---|
Can only be read and cannot be written using fwrite | |
Can be read and written | |
Only write function | |
Readable and writeable |
Mode | Summary |
---|---|
x | Every time you write, the contents of the original file will be deleted. If the file does not exist, it will be created. |
a | Every time you write, it will be written to the end of the file. Additional content |
# Note: a+ is an enhanced additional function. It can also be used when it can be read.
The difference between x mode and w mode
Let’s try this code again and change it to x mode:
<?php $filename = 'test.txt'; $fp= fopen($filename, "x"); $len = fwrite($fp,'读大学迷茫了,PHP中文网学PHP给你希望'); echo $len .'字节被写入了\n'; ?>
We will find:
1 . An error will be reported when the file exists
2. If you change $filename to another file name, it will be fine. However, when refreshing again, an error was reported
3.x+ is an enhanced x mode. Can also be used when reading.
Continuing Learning- Course Recommendations
- Courseware download
-
ElementaryImperial CMS enterprise imitation website tutorial
3048 people are watching -
ElementaryNewbies with zero foundation in WordPress build personal blogs and corporate websites
6743 people are watching -
ElementaryUltimate CMS zero-based website building instruction video
2724 people are watching -
ElementaryFront-end project-Shangyou [HTML/CSS/JS technology comprehensive practice]
3117 people are watching -
IntermediateVue3.0 from 0 to build a universal backend management system project practice
5351 people are watching -
ElementaryZero-based front-end course [Vue advanced learning and practical application]
2821 people are watching -
ElementaryWEB front-end tutorial [HTML5+CSS3+JS]
3506 people are watching -
ElementaryQuick introduction to apipost
2161 people are watching -
IntermediateVue3+TypeScript practical tutorial-enterprise-level project practice
3208 people are watching -
ElementaryLet's briefly talk about starting a business in PHP
17423 people are watching -
IntermediateVUE e-commerce project (front-end & back-end dual project actual combat)
3828 people are watching -
ElementaryApipost practical application [api, interface, automated testing, mock]
2265 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)