php检查nfs目录下文件是否可读
2024-11-24 / PHP / 227 次围观 / 0 次吐槽 /PHP
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function readFileContent($filePath) {
// 初始化返回数组
$result = array();
$result["status"] = "error";
$result["message"] = "";
$result["time"] = date('Y-m-d H:i:s');
$result["trace"] = "";
// 获取open_basedir设置
$openBasedir = ini_get('open_basedir');
echo "当前open_basedir设置: " . $openBasedir . "\n";
try {
// 检查参数
if (empty($filePath)) {
throw new Exception("文件路径不能为空");
}
// 检查文件路径是否在允许的目录中
$allowed = false;
$basedirs = explode(':', $openBasedir);
foreach ($basedirs as $dir) {
if (strpos($filePath, $dir) === 0) {
$allowed = true;
break;
}
}
if (!$allowed) {
throw new Exception("文件路径不在允许访问的目录中。\n".
"允许的目录: " . $openBasedir . "\n" .
"当前路径: " . $filePath);
}
clearstatcache();
// 检查文件是否存在
if (!file_exists($filePath)) {
throw new Exception("文件不存在\n路径: " . $filePath);
}
// 获取文件基本信息
$filePerms = @decoct(fileperms($filePath));
// 检查是否有读取权限
if (!is_readable($filePath)) {
$currentUser = '';
if (function_exists('posix_getpwuid')) {
$userInfo = @posix_getpwuid(posix_geteuid());
$currentUser = isset($userInfo['name']) ? $userInfo['name'] : 'unknown';
}
throw new Exception("没有读取权限\n路径: " . $filePath .
"\n权限: " . $filePerms .
"\n当前用户: " . $currentUser);
}
// 读取文件内容
$content = @file_get_contents($filePath);
if ($content === false) {
$error = error_get_last();
throw new Exception("读取文件失败\n路径: " . $filePath . "\n错误: " .
(isset($error['message']) ? $error['message'] : '未知错误'));
}
$result["status"] = "success";
$result["content"] = $content;
$result["file_size"] = filesize($filePath);
$result["last_modified"] = date("Y-m-d H:i:s", filemtime($filePath));
} catch (Exception $e) {
error_log("文件读取错误: " . $e->getMessage());
$result["status"] = "error";
$result["message"] = $e->getMessage();
$result["trace"] = $e->getTraceAsString();
}
return $result;
}
// 使用示例 - 使用允许访问的目录
$filePath = "/home/wwwroot/web/test.cheug.com/public_html/test.txt"; // 修改为允许访问的路径
echo "准备读取文件: " . $filePath . "\n";
$result = readFileContent($filePath);
// 格式化输出结果
echo "\n最终执行结果:\n";
echo "状态: " . (isset($result["status"]) ? $result["status"] : "未知") . "\n";
if (isset($result["status"]) && $result["status"] == "success") {
echo "文件读取成功!\n";
echo "文件大小: " . $result["file_size"] . " 字节\n";
echo "最后修改时间: " . $result["last_modified"] . "\n";
echo "文件内容:\n" . $result["content"] . "\n";
} else {
echo "错误信息:\n" . (isset($result["message"]) ? $result["message"] : "未知错误") . "\n";
echo "发生时间: " . (isset($result["time"]) ? $result["time"] : "") . "\n";
if (isset($result["trace"]) && !empty($result["trace"])) {
echo "错误堆栈:\n" . $result["trace"] . "\n";
}
}
?>
PHP
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function readFileContent($filePath) {
// 初始化返回数组
$result = array();
$result["status"] = "error";
$result["message"] = "";
$result["time"] = date('Y-m-d H:i:s');
$result["trace"] = "";
// 调试信息
echo "开始执行函数...\n";
echo "检查文件路径: " . $filePath . "\n";
if (!function_exists('posix_getpwuid')) {
$result["message"] = "服务器不支持posix函数,请检查PHP配置";
return $result;
}
try {
// 检查参数
if (empty($filePath)) {
throw new Exception("文件路径不能为空");
}
clearstatcache();
// 检查文件是否存在
if (!file_exists($filePath)) {
throw new Exception("文件不存在\n路径: " . $filePath);
}
echo "文件存在,继续检查...\n";
// 获取文件基本信息
$filePerms = @decoct(fileperms($filePath));
echo "文件权限: " . $filePerms . "\n";
// 获取文件系统信息
echo "尝试获取文件系统信息...\n";
// 方法1:使用mount命令
$mountInfo = @shell_exec("mount 2>&1");
echo "挂载信息:\n" . $mountInfo . "\n";
// 方法2:使用df命令(不带-T参数)
$dfInfo = @shell_exec("df " . escapeshellarg($filePath) . " 2>&1");
echo "df命令输出:\n" . $dfInfo . "\n";
// 方法3:直接读取/proc/mounts
if (file_exists('/proc/mounts')) {
$procMounts = @file_get_contents('/proc/mounts');
echo "proc/mounts内容:\n" . $procMounts . "\n";
}
// 检查是否有读取权限
if (!is_readable($filePath)) {
$currentUser = @posix_getpwuid(posix_geteuid());
$currentUserName = isset($currentUser['name']) ? $currentUser['name'] : 'unknown';
throw new Exception("没有读取权限\n路径: " . $filePath .
"\n权限: " . $filePerms .
"\n当前用户: " . $currentUserName);
}
echo "文件可读,准备读取内容...\n";
// 读取文件内容
$content = @file_get_contents($filePath);
if ($content === false) {
$error = error_get_last();
throw new Exception("读取文件失败\n路径: " . $filePath . "\n错误: " .
(isset($error['message']) ? $error['message'] : '未知错误'));
}
echo "文件读取成功\n";
$result["status"] = "success";
$result["content"] = $content;
$result["file_size"] = filesize($filePath);
$result["last_modified"] = date("Y-m-d H:i:s", filemtime($filePath));
} catch (Exception $e) {
echo "捕获到异常: " . $e->getMessage() . "\n";
error_log("文件读取错误: " . $e->getMessage());
$result["status"] = "error";
$result["message"] = $e->getMessage();
$result["trace"] = $e->getTraceAsString();
}
echo "函数执行完毕\n";
return $result;
}
// 使用示例
$filePath = "/cfile/web/files/article/txt/10/10507/7405497.txt"; // 使用实际的文件路径
echo "准备读取文件: " . $filePath . "\n";
$result = readFileContent($filePath);
if (!is_array($result)) {
die("函数返回值不是数组\n");
}
// 格式化输出结果
echo "\n最终执行结果:\n";
echo "状态: " . (isset($result["status"]) ? $result["status"] : "未知") . "\n";
if (isset($result["status"]) && $result["status"] == "success") {
echo "文件读取成功!\n";
echo "文件大小: " . $result["file_size"] . " 字节\n";
echo "最后修改时间: " . $result["last_modified"] . "\n";
echo "文件内容:\n" . $result["content"] . "\n";
} else {
echo "错误信息:\n" . (isset($result["message"]) ? $result["message"] : "未知错误") . "\n";
echo "发生时间: " . (isset($result["time"]) ? $result["time"] : "") . "\n";
if (isset($result["trace"]) && !empty($result["trace"])) {
echo "错误堆栈:\n" . $result["trace"] . "\n";
}
}
?>
- 上一篇:使用ipmiutil冷重启bmc
- 下一篇:船说cms修改代码支持mysql持久化
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.