首页  >  文章  >  后端开发  >  PHP读取文件

PHP读取文件

WBOY
WBOY原创
2024-08-29 13:02:30254浏览

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 file Functions下一篇:PHP Write File