This commit is contained in:
parent
077bd47cb2
commit
59b98c073b
|
|
@ -10,29 +10,6 @@ class LogMiddleware implements MiddlewareInterface
|
|||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
|
||||
function setFileWritePermission($filePath, $permission = 0664) {
|
||||
// 检查文件是否存在
|
||||
if (!file_exists($filePath)) {
|
||||
// 如果文件不存在,先创建文件
|
||||
if (!touch($filePath)) {
|
||||
error_log("无法创建文件: $filePath");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 检查当前是否已有写入权限
|
||||
if (is_writable($filePath)) {
|
||||
return true; // 已有写入权限,无需修改
|
||||
}
|
||||
// 尝试设置权限
|
||||
if (chmod($filePath, $permission)) {
|
||||
return true;
|
||||
} else {
|
||||
error_log("无法给文件设置权限:" . $filePath . "权限值:" . $permission);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
$logFile = '/rh/webman_duanju/runtime/logs/webman-' . date('Y-m-d') . '.log';
|
||||
// 确保目录存在
|
||||
|
|
@ -40,12 +17,25 @@ class LogMiddleware implements MiddlewareInterface
|
|||
if (!is_dir($logDir)) {
|
||||
mkdir($logDir, 0755, true); // 创建目录,权限755
|
||||
}
|
||||
// 给文件设置写入权限(使用0664权限,比0666更安全)
|
||||
setFileWritePermission($logFile);
|
||||
|
||||
|
||||
|
||||
$filePath = $logFile;
|
||||
// 检查文件是否存在
|
||||
if (!file_exists($filePath)) {
|
||||
// 如果文件不存在,先创建文件
|
||||
if (!touch($filePath)) {
|
||||
error_log("无法创建文件: $filePath");
|
||||
}
|
||||
}
|
||||
// 检查当前是否已有写入权限
|
||||
if (is_writable($filePath)) {
|
||||
// 已有写入权限,无需修改
|
||||
}else {
|
||||
// 尝试设置权限
|
||||
if (chmod($filePath, 0664)) {
|
||||
} else {
|
||||
error_log("无法给文件设置权限:" . $filePath . "权限值:" . 0664);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue