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 rewritten
2. 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:
ModeDescription | |
rCan only be read and cannot be written using fwrite | |
r+Can be read and written | |
wOnly write function | |
w+Readable and writeable | |
Let’s prove it through experiments:
<?php
$filename = 'test.txt';
$fp= fopen($filename, "r+");
$len = fwrite($fp, '我是一只来自南方的狼,一直在寻找心中的花姑娘');
fclose($fp);
print $len .'字节被写入了\n';
?>
You can remove the + sign after r during the experiment.
Through experiments, we have indeed found that using r mode, data can be written when the file is saved. If only r is used, the writing is unsuccessful.
The difference between a mode and w mode
The same is the code below, we change it to a mode.
<?php
$filename = 'test.txt';
$fp= fopen($filename, "a");
$len = fwrite($fp,'读大学迷茫了,PHP中文网学PHP给你希望');
echo $len .'字节被写入了\n';
?>
Open the webpage and execute this code, you will find: every time you refresh, there will be an extra paragraph in the file
: If you are confused in college, learning PHP on the Chinese PHP website will give you hope.
Summary:
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.
Next Section<?php
$filename = 'test.txt';
$fp= fopen($filename, "x");
$len = fwrite($fp,'读大学迷茫了,PHP中文网学PHP给你希望');
echo $len .'字节被写入了\n';
?>
- Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...