PHP中的readfile()函数是一个内置函数,用于读取文件并将其写入输出缓冲区。文件名作为参数发送到readfile()函数,它返回成功读取的字节数,如果失败,则返回FALSE和错误。
通过在函数名称前添加“ @”,可以隐藏错误输出。
用法:
readfile(filename, include_path, context)
使用的参数:
PHP中的readfile()函数接受三个参数。
filename :它是指定文件名的必需参数。
include_path:它是一个可选参数,如果要在PHP的include_path中搜索文件,可以将其设置为1。
context :它是一个可选参数,用于指定流的行为。
返回值:
它返回成功读取的字节数,如果失败,则返回FALSE和错误。
Note:URL can be used as a filename with this function if the fopen wrappers have been enabled.
错误与异常
在调用Readfile()函数之前关闭输出缓冲可能有助于将较大的文件读入内存。
例子:
Input : echo readfile("gfg.txt");
Output : A computer portal for geeks!
Input : $myfile = @readfile("gfg.txt");
if (!$myfile)
{
print "File could not be opened";
}
Output : A computer portal for geeks!
以下示例程序旨在说明readfile()函数。
假设有一个名为“gfg.txt”的文件
程序1
// writing file contents on the output
// buffer using readfile() function
echo readfile("gfg.txt");
?>
输出:
A computer portal for geeks!
程序2
// writing file contents on the output
// buffer using readfile() function
$myfile = @readfile("gfg.txt");
if (!$myfile)
{
print "File could not be opened";
}
?>
输出:
A computer portal for geeks!