Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-01 13:12:44 09月19日 Update

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!

Tags: Files functions patterns articles parameters data maximum good value creativity interest at the same time characters strings bytes pointers more friends knowledge ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Shulou Information vpn Xiaomi