Home  >  Article  >  Backend Development  >  Basic knowledge of PHP file processing (summary)

Basic knowledge of PHP file processing (summary)

藏色散人
藏色散人Original
2019-01-31 15:29:512990browse



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.

Basic knowledge of PHP file processing (summary)

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”,&#39;w&#39;); 
?>

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, &#39;r&#39; ); 
$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", &#39;w&#39;); 
$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", &#39;r&#39;); 
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!

Statement:
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