PHP is a popular server-side scripting language that is widely used in web development. In PHP, file operations are very common needs, such as reading/writing files, uploading/downloading files, etc. In order to realize these operations, PHP provides file inflow and outflow functions, that is, after opening the file, you can read content from the file or write content to the file. This article will introduce these functions in detail.
1. File flow function
fopen() function is to open a file and position the pointer to the file Starting point. There are many ways to open, the commonly used ones include read mode and write mode.
Example: Open a file named test.txt, use read mode:
$fp=fopen("test.txt","r");
At this time, the $fp variable stores the open file pointer, that is, you can use other file flow functions to Read the file contents.
The fread() function can read data of a specified length from an opened file. It accepts two parameters, one is the file pointer and the other is the length to be read.
Example: Read 10 bytes of data from the test.txt file:
$fp=fopen("test.txt","r"); if($fp){ $content=fread($fp,10); echo $content; }
fgets() function and The fread() function is similar, except that it reads the file content line by line. It also accepts two parameters, one is the file pointer and the other is the number of lines to read.
Example: Read a line of data from the test.txt file:
$fp=fopen("test.txt","r"); if($fp){ $content=fgets($fp); echo $content; }
The file() function can read the entire file The contents are read into an array, with each row as an element of the array.
Example: Read the contents of the test.txt file into an array:
$fileArr=file("test.txt"); print_r($fileArr);
The file_get_contents() function can The contents of the entire file are read into a string.
Example: Read the contents of the test.txt file into a string:
$fileContent=file_get_contents("test.txt"); echo $fileContent;
2. File outflow function
The fwrite() function can write data to an opened file. It accepts two parameters, one is the file pointer and the other is the written data.
Example: Write "Hello World" to the test.txt file:
$fp=fopen("test.txt","w"); if($fp){ fwrite($fp,"Hello World"); fclose($fp); }
fputs() function and fwrite The () function is similar and also writes data to the opened file, but its parameter order and writing method are slightly different.
Example: Write "Hello World" to the test.txt file:
$fp=fopen("test.txt","w"); if($fp){ fputs($fp,"Hello World"); fclose($fp); }
The file_put_contents() function can Data is written to a file, and if the file does not exist, a new file is automatically created. It accepts two parameters, one is the file name and the other is the data to be written.
Example: Write "Hello World" into the test.txt file:
file_put_contents("test.txt","Hello World");
The role of the fclose() function is to close an open file. When a large number of files are open, closing the files in time can effectively save system resources.
Example: Close the test.txt file:
$fp=fopen("test.txt","r"); //其他文件读取操作 fclose($fp);
The above is the introduction and usage examples of the file inflow and outflow functions. It should be noted that file operations involve the occupation of system resources and file operations. Permissions need to follow corresponding security principles and best practices when using them.
The above is the detailed content of Detailed explanation of PHP file inflow and outflow functions. For more information, please follow other related articles on the PHP Chinese website!