如何使用PHP file()和readfile()函数读取PHP文件?

本文介绍了PHP中用于读取文件的file()和readfile()函数。file()函数返回一个包含文件所有行的数组,而readfile()则将整个文件内容作为字符串输出。通过实例展示了如何使用这两个函数读取文本或PHP文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 set

    INCLUDE_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 set

    INCLUDE_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);

?>
file() Function with PHP File
file() Function with PHP File
file()PHP文件的功能

带有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);

?>
readfile() Function with PHP File
readfile() Function with PHP File
带有PHP文件的readfile()函数
LEARN MORE  How To Find Directory In Linux?
了解更多如何在Linux中查找目录?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值