搜索

PHP readfile 基本上是 PHP 库中的一个内置函数,用于读取文件然后将其写入输出缓冲区。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

readfile ( string $file_name [, boolean $path = FALSE [, resource $context ]] ) : int

语法中使用的参数:

  • 文件名:这是必填字段,我们在其中提供要读取的文件的名称。
  • 路径:这是一个可选参数,是一个布尔值,如果需要在提供的路径中搜索文件,则可以将其设置为 true 或 false。
  • context: 这也是一个可选字段,用于指定文件句柄的上下文。基本上,上下文是能够更改该流的行为的对象的集合。如果成功则返回从文件中读取的字节数,如果读取失败则返回 false。

PHP 读取文件的方法

除了 readfile() 函数之外,以下是一些可用于对文件执行各种操作的其他函数。

1.文件()

此函数还用于读取文件,它将完整的文件读取到数组中。

语法:

file ( string $file_name [, int $flag = 0 [, resource $context ]] ) : array

其中file_name是要读取的文件的路径。标志是可选字段,可以从以下常量中选择:

  • FILE_USE_INCLUDE_PATH: 在给定路径中搜索相应的文件。
  • FILE_IGNORE_NEW_LINES: 在每个数组元素的最后省略一个新行。
  • FILE_SKIP_EMPTY_LINES: 跳过空行。

成功时返回数组中存在的文件,失败时返回 false。

2. fopen()

此函数可用于打开文件和 URL。

fopen ( string $file_name , string $mode [, bool $use_include_path = FALSE [, resource $context ]] ) : resource
  • 如果 file_name 是 URL,则 PHP 会在协议处理程序(也称为包装器)中搜索它。 PHP 将发出通知来帮助跟踪脚本中可能存在的问题,然后继续将其视为正常的常规 file_name。
  • 假设如果 file_name 是本地文件,则 PHP 尝试在该文件上打开相同的流。仅当授予其访问所需的权限时,PHP 才能访问该文件。
  • 假设给定的 file_name 是一个已注册的协议,并且如果该协议已注册为网络 URL,则第一个 PHP 确保启用了allow_url_fopen。如果禁用,它将发出警告并失败。

模式:此参数提到了需要授予流的访问权限类型。下面是一些重要的模式:

  • r – 仅读取模式
  • r+ – 只能读和写
  • w – 仅写入模式

如果成功则返回文件指针资源,如果失败则返回 false。

3. fread()

此函数用于二进制安全文件读取。

语法:

fread ( resource $handle , int $length ) : string

句柄用于引用文件指针。

文件将被读取,直到达到以下条件之一:长度(以字节为单位)必须已读取、达到 EOF、发生套接字超时。 fgets()、fscanf()、ftell()、fwrite()、fopen()、fsockopen() 是 PHP 中用于文件操作的其他一些函数。

PHP 读取文件示例

下面给出的是 PHP 读取文件的示例:

示例#1

代码:

<?php // it is writing content of file to output
// buffer used in readfile() function
echo readfile("file.txt");
?>

输出:

PHP读取文件

这显示了读取本地路径中存在的文件的基本示例。确保创建了 readfile() 函数参数中指定的文件名,并且要读取的内容存在于文件内。当使用 readfile() 函数时,它会读取文件的内容并在输出中打印。

示例#2

代码:

<?php / file contents written on output
// buffer by readfile() function
$file = @readfile("file.txt");
if (!$file)
{
print "File could not be opened";
}
?>

输出:

PHP读取文件

之前的输出是一个没有任何条件的简单示例。在此示例中,让我们看看如何使用某些条件读取并显示文件的输出。我们使用 if 语句来打印假设文件不存在。

示例 #3

代码:

<?php $file_name = "file.txt";
$fh = fopen($file_name, 'r');
$data = fread($fh, filesize($file_name));
fclose($fh);
echo $data;
?>

输出:

PHP读取文件

In this example, we are combining the use of multiple file read functions. As in all the above examples, first, we are giving the file name which needs to be read from. Then the mode of operation, ‘r’ is given to them indicating it can only be read. filesize() function takes the filename and returns the size of the file along with its data and assigns it to $data variable. By using fclose() function we are closing that file. Finally, the data is printed as output.

Example #4

Code:

<?php $file_name = "file.txt";
$file = fopen( $file_name , "r" );
if( $file == false ) {
echo ( "Error in opening file" );
exit();
}
$size = filesize( $file_name );
$filetext = fread( $file, $size);
fclose( $file );
echo ( "The size of input file in bytes is : $size\n" );
echo ("Printing details of file:\n");
echo ( $filetext );
?>

Output:

PHP读取文件

Before running the code, make sure that the file to be read file.txt is created in the local file path. In this example first, we are declaring the file name to be read and opening that with the function fopen(). Suppose the file does not exist, using if condition we are throwing an error message. Finally, we are printing the file size and the content present in the input file.

Conclusion

As seen from all the above examples, readfile() is one of the main functions of PHP used for reading the file name specified in this function. Apart from readfile() we have covered a few other file operations which perform similar actions such as fopen, file, fread, fgets, fgetss, ftell, etc. A combination of all of these are basically used in accessing and performing operations on the input file.

以上是PHP读取文件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版