Home > php教程 > php手册 > body text

File writing code for PHP file reading and writing operations

WBOY
Release: 2016-10-28 15:03:30
Original
1124 people have browsed it

In PHP website development, there are usually two ways to store data. One is stored in text files, such as txt files, and the other is stored in databases, such as Mysql. Compared with database storage, file storage has no advantages. However, file reading and writing operations are still used from time to time in basic PHP development. Today I will share with you a file writing operation tutorial on how to use PHP technology to realize file reading and writing. It can also be regarded as an introduction to PHP file reading and writing operations.
The operation of writing data to a file mainly involves three steps and some file operation functions are as follows:
1. Open the file (file operation function: fopen)
2. Write the file (file operation function: fwrite, etc.)
3. Close File (file operation function: fclose)
The following will explain through the file reading and writing operation code example tutorial
Basic PHP file writing operation function fopen, fwrite, fclose application tutorial

<?<span style="color: #000000">   
@</span><span style="color: #800080">$fp</span> = <span style="color: #008080">fopen</span>("leapsoulcn.txt","w"<span style="color: #000000">);   
</span><span style="color: #0000ff">if</span>(!<span style="color: #800080">$fp</span><span style="color: #000000">){   
</span><span style="color: #0000ff">echo</span> "system error"<span style="color: #000000">;   
</span><span style="color: #0000ff">exit</span><span style="color: #000000">();   
}</span><span style="color: #0000ff">else</span><span style="color: #000000"> {   
</span><span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1343.html  </span>
<span style="color: #800080">$fileData</span> = "domain"."\t"."www.leapsoul.cn"."\n"<span style="color: #000000">;   
</span><span style="color: #800080">$fileData</span> = <span style="color: #800080">$fileData</span>."description"."\t"."PHP网站开发教程网,面向PHP初学者的PHP教程网。"."\n"<span style="color: #000000">;   
</span><span style="color: #800080">$fileData</span> = <span style="color: #800080">$fileData</span>."title"."\t"."本文主要讲述PHP文件读写操作中最基本的文件写入教程。"<span style="color: #000000">;   
</span><span style="color: #008080">fwrite</span>(<span style="color: #800080">$fp</span>,<span style="color: #800080">$fileData</span><span style="color: #000000">);   
</span><span style="color: #008080">fclose</span>(<span style="color: #800080">$fp</span><span style="color: #000000">);   
}   
</span>?>   
Copy after login

Note: In this file reading and writing example code, the main function is to write two lines of text to the file.
Knowledge points:
 1. Use the fopen function to open a file. When applying the fopen function to prepare to open a file, you first need to clarify:
 What to open the file for? Is it reading the data in the file, writing the data to the file, or reading and writing the file?
 In addition, you need to consider if relevant data already exists in the file, should you overwrite the data in the original file, or just add new data to the end of the file?
 These issues involve files in the fopen function in PHP file read and write operations. For the application of the pattern, the fopen function prototype is as follows:

<span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1344.html  </span>
<span style="color: #008080">fopen</span>(filename,mode,include_path,context)   
Copy after login

When calling the file operation function fopen(), you usually need to pass two or three parameters.
filename: Specifies the file or URL to be opened. You can specify the absolute path of the file, usually C: for Windows and / for Unix. You can also open remote files through URLs. The file written here is in the same directory as the PHP file writing code file I placed.
mode: Specifies the type of access required to the file/stream. That is, the mode in which the file is opened.
include_path: optional. If you need to search for files in include_path, you can set this parameter to 1 or TRUE.
Explanation of commonly used fopen file operation modes
"r" - Open the file in read-only mode and start reading from the file header.
"r+" - Open the file for reading and writing.
"w" - Open the file for writing and start writing from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
"w+" - Open the file for reading and writing, starting from the beginning of the file. If the file does not exist, try to create it. If the file exists, delete the contents of the file first.
"a" - Open for writing, append writing starting from the end of the file. If the file does not exist then attempts to create it.
"a+" - Open for reading and writing, append writing or reading from the end of the file. If the file does not exist then attempts to create it.
Note: When performing file read and write operations, you must ensure that the file you open has the corresponding read and write permissions, otherwise fopen will report an error. You can use @ to suppress errors and handle them appropriately.
2. After using the file operation function fopen to open the file, you need to assign a value to the variable and then write it to the file pointer pointed to by $fp. In the above example of the PHP file writing operation tutorial, I used line-by-line storage. That is, newline storage, mainly using n as the newline separator.
The prototype of the fwrite file writing function is as follows:

<span style="color: #008080">fwrite</span>(fp,<span style="color: #0000ff">string</span>,length)   
Copy after login

Here you can also use the file writing function fputs, which is an alias function of fwrite. Its function and usage are the same as fwrite.
In the file writing function fwrite, length is an optional option. It is mainly used to set the maximum number of characters to write to the file. If this parameter is set, fwrite will write the specified length in the specified file according to the set length. character. fwrite() returns the number of characters written to the file, or false if an error occurs.
After the file writing operation is completed, the file handle needs to be closed, otherwise it will occupy system resources, etc. You can use the fclose($fp) function to accomplish this. Returns true if the file is closed successfully, otherwise returns false.

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!