Detailed explanation of PHP file inflow and outflow functions

王林
Release: 2023-06-15 21:48:01
Original
1414 people have browsed it

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

  1. fopen()

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");
Copy after login

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.

  1. fread()

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;
}
Copy after login
  1. fgets()

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;
}
Copy after login
  1. file()

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);
Copy after login
  1. file_get_contents()

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;
Copy after login

2. File outflow function

  1. fwrite()

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);
}
Copy after login
  1. fputs()

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);
}
Copy after login
  1. file_put_contents()

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");
Copy after login
  1. fclose()

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template