get_file()
云策文档标注
概述
get_file() 函数用于读取指定路径文件的内容,如果文件不存在或不是有效文件则返回空字符串。它基于 PHP 的 file_get_contents() 实现,并包含路径验证。
关键要点
- 函数接受一个路径参数 $path,使用 realpath() 解析为绝对路径。
- 通过 @is_file() 检查路径是否为有效文件,如果不是则返回空字符串 ''。
- 使用 @file_get_contents() 读取文件内容,错误抑制符 @ 用于避免潜在警告。
- 函数返回文件内容字符串,失败时返回空字符串。
原文内容
Source
function get_file( $path ) {
$path = realpath( $path );
if ( ! $path || ! @is_file( $path ) ) {
return '';
}
return @file_get_contents( $path );
}