Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create, open, read, write and close files with PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how PHP to create, open, read, write and close file operations, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Open file: use the open () function to open the file, and the second parameter specifies the mode of opening the file, such as read (r).

Read the file: use the fread () function to read the open file, and the second parameter specifies the maximum number of bytes to be read.

Close files: it is a good programming practice to use the fclose () function to close open files and close them when you are done with them.

$myfile = fopen ('testfile.txt', 'r') or die (' Unable to open fileholders'); echo fread ($myfile, filesize ('testfile.txt')); fclose ($myfile)

Create a file: when you use the fopen () function to open a file that does not exist, this function creates a file if the file mode is write (w) or add (a).

Write to the file: use the fwrite () function to write to the file, and the second argument is the string being written.

Overwrite file: when writing a file using the fwrite () function, if the file mode is write (w), all existing data will be erased and start with a new file.

Append file: when writing a file using the fwrite () function, if the file mode is added (a), all existing data will be retained and the file pointer starts at the end of the file.

$myfile = fopen ("newfile.txt", "a") or die ("Unable to open file!"); $txt = "Bill Gates is one of the richest people.\ n"; fwrite ($myfile, $txt); $txt = "Steve Jobs is the CEO of Apple.\ n"; fwrite ($myfile, $txt); fclose ($myfile) Thank you for reading this article carefully. I hope the article "how to create, open, read, write and close files in PHP" shared by the editor will be helpful to everyone. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report