Home > Article > Backend Development > Basic knowledge of PHP file processing (summary)
Any application requires file handling. For certain tasks to be accomplished, files need to be processed. File handling in PHP Similar to file handling which is done by using any programming language like C, PHP has many functions to handle normal files.
These functions are:
1.fopen() - The PHP fopen() function is used to open a file. The first parameter of fopen() contains the name of the file to be opened, and the second parameter specifies the mode in which the file needs to be opened. For example,
<?php $file = fopen(“demo.txt”,'w'); ?>
The file can be opened using any of the following modes:
"w" - Open the file for writing. If the file does not exist, create a new file or delete the file contents if the file already exists.
"r" - The file is opened for read-only use.
"a" - The file is opened for writing. The file pointer points to the end of the file. Preserve existing data in the file.
"w" - Open the file for reading and writing. If the file does not exist, create a new file or delete the file contents if the file already exists.
"r " - Open file for reading/writing.
"a " - Open file for writing/reading. The file pointer points to the end of the file. Preserve existing data in the file. Create a new file if the file does not exist.
"x" - Create new file for write only.
2.fread() - - After using fopen() to open the file, use fread() to read the data content. It requires two parameters. One is the file pointer and the other is the file size in bytes, for example,
<?php $filename = "demo.txt"; $file = fopen( $filename, 'r' ); $size = filesize( $filename ); $filedata = fread( $file, $size ); ?>
3.fwrite() - You can use the fwrite() function to create a new file or copy text Append to existing file. The parameters of the fwrite() function are the file pointer and the text to be written to the file. It can contain an optional third parameter, which specifies the length of text to be written, for example,
<?php $file = fopen("demo.txt", 'w'); $text = "Hello world\n"; fwrite($file, $text); ?>
4.fclose() - Use the fclose() function to close the file. Its parameters are files that need to be closed, for example,
<?php $file = fopen("demo.txt", 'r'); fclose($file); ?>
This article is an introduction to the basic knowledge of PHP file processing. I hope it will be helpful to friends in need!
The above is the detailed content of Basic knowledge of PHP file processing (summary). For more information, please follow other related articles on the PHP Chinese website!