PHP programming language provides file()
and readfile()
functions in order to read a file. In this tutorial, we will learn how to use file() and readfile() functions in order to read text or similar ASCII files like PHP files.
PHP编程语言提供了file()
和readfile()
函数以读取文件。 在本教程中,我们将学习如何使用file()和readfile()函数来读取文本或类似ASCII文件(如PHP文件)。
file()函数和语法 (file() Function and Syntax)
file() function has a very basic syntax where some of the parameters are optional.
file()函数具有非常基本的语法,其中某些参数是可选的。
file(PATH,INCLUDE_PATH,CONTEXT)
- PATH is used to specify the path and file name we want to read PATH用于指定我们要读取的路径和文件名
- INCLUDE_PATH is an optional parameter and if set to 1 the path configuration in the `php.ini` will we setINCLUDE_PATH是一个可选参数,如果设置为1,我们将设置`php.ini`中的路径配置
- CONTEXT is an optional parameter and specifies the handle of the file if its already opened.CONTEXT是一个可选参数,用于指定文件的句柄(如果已打开)。
readfile()函数和语法 (readfile() Function and Syntax)
readfile() function uses the same syntax and parameters with the file() function which is like below.
readfile()函数使用与file()函数相同的语法和参数,如下所示。
file(PATH,INCLUDE_PATH,CONTEXT)
- PATH is used to specify the path and file name we want to read PATH用于指定我们要读取的路径和文件名
- INCLUDE_PATH is an optional parameter and if set to 1 the path configuration in the `php.ini` will we setINCLUDE_PATH是一个可选参数,如果设置为1,我们将设置`php.ini`中的路径配置
- CONTEXT is an optional parameter and specifies the handle of the file if its already opened.CONTEXT是一个可选参数,用于指定文件的句柄(如果已打开)。
file()PHP文件的功能 (file() Function with PHP File)
In this part, we will make an example with the file () function where we will read a file named example.txt
like below. Then we will put the read content an array named text
. We will print the $text with the print_r() function.
在这一部分中,我们将使用file()函数作为示例,我们将读取一个名为example.txt
的文件,如下所示。 然后,我们将读取的内容放入名为text
的数组。 我们将使用print_r()函数打印$ text。
<?php
$text=file("example.txt");
print_r($text);
?>

带有PHP文件的readfile()函数(readfile() Function with PHP File)
readfile() function reads the whole file and put into the given variable as a complete string variable. The difference with the read() function is the output is not an array in line by line.
readfile()函数读取整个文件,并将其作为完整的字符串变量放入给定变量中。 与read()函数的区别在于输出不是逐行的数组。
<?php
$text=file("example.txt");
print_r($text);
?>

翻译自: https://www.poftut.com/how-to-use-php-file-and-readfile-functions-to-read-php-file/