PHP readfile 基本上是 PHP 库中的一个内置函数,用于读取文件然后将其写入输出缓冲区。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
readfile ( string $file_name [, boolean $path = FALSE [, resource $context ]] ) : int
语法中使用的参数:
除了 readfile() 函数之外,以下是一些可用于对文件执行各种操作的其他函数。
此函数还用于读取文件,它将完整的文件读取到数组中。
语法:
file ( string $file_name [, int $flag = 0 [, resource $context ]] ) : array
其中file_name是要读取的文件的路径。标志是可选字段,可以从以下常量中选择:
成功时返回数组中存在的文件,失败时返回 false。
此函数可用于打开文件和 URL。
fopen ( string $file_name , string $mode [, bool $use_include_path = FALSE [, resource $context ]] ) : resource
模式:此参数提到了需要授予流的访问权限类型。下面是一些重要的模式:
如果成功则返回文件指针资源,如果失败则返回 false。
此函数用于二进制安全文件读取。
语法:
fread ( resource $handle , int $length ) : string
句柄用于引用文件指针。
文件将被读取,直到达到以下条件之一:长度(以字节为单位)必须已读取、达到 EOF、发生套接字超时。 fgets()、fscanf()、ftell()、fwrite()、fopen()、fsockopen() 是 PHP 中用于文件操作的其他一些函数。
下面给出的是 PHP 读取文件的示例:
代码:
<?php // it is writing content of file to output // buffer used in readfile() function echo readfile("file.txt"); ?>
输出:
这显示了读取本地路径中存在的文件的基本示例。确保创建了 readfile() 函数参数中指定的文件名,并且要读取的内容存在于文件内。当使用 readfile() 函数时,它会读取文件的内容并在输出中打印。
代码:
<?php / file contents written on output // buffer by readfile() function $file = @readfile("file.txt"); if (!$file) { print "File could not be opened"; } ?>
输出:
之前的输出是一个没有任何条件的简单示例。在此示例中,让我们看看如何使用某些条件读取并显示文件的输出。我们使用 if 语句来打印假设文件不存在。
代码:
<?php $file_name = "file.txt"; $fh = fopen($file_name, 'r'); $data = fread($fh, filesize($file_name)); fclose($fh); echo $data; ?>
输出:
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.
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:
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.
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中文网其他相关文章!