Files
jj2/utils/FileManager.php
LL 0cfefbebd8 feat: 添加基金监控系统基础功能
添加基金监控系统相关文件,包括邮件发送功能、基金数据配置、测试脚本等。主要包含以下内容:

1. 添加PHPMailer库及相关语言文件
2. 添加基金配置数据文件(fund_config.json, fund_names.json等)
3. 添加邮件发送测试脚本(test_email.php, test_fund_email.php等)
4. 添加.gitignore文件忽略不必要的文件
5. 添加composer.json配置依赖

Signed-off-by: LL <LL>
2025-12-12 14:14:07 +08:00

87 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 文件管理工具类
* 提供通用的文件和目录操作功能,减少代码重复
*/
class FileManager {
/**
* 确保目录存在,如果不存在则创建
* @param string $dirPath 目录路径
* @param int $mode 目录权限默认0755
* @return bool 创建成功或目录已存在返回true失败返回false
*/
public static function ensureDirExists($dirPath, $mode = 0755) {
if (!is_dir($dirPath)) {
return mkdir($dirPath, $mode, true);
}
return true;
}
/**
* 加载JSON文件内容
* @param string $filePath 文件路径
* @param mixed $default 默认值(文件不存在或解析失败时返回)
* @param callable|null $initCallback 初始化回调函数(文件不存在时调用)
* @return mixed 解析后的JSON数据或默认值
*/
public static function loadJsonFile($filePath, $default = [], callable $initCallback = null) {
if (!file_exists($filePath)) {
if ($initCallback) {
call_user_func($initCallback);
} else {
return $default;
}
}
$data = file_get_contents($filePath);
if (!$data) {
return $default;
}
$json = json_decode($data, true);
return is_array($json) ? $json : $default;
}
/**
* 保存JSON数据到文件
* @param string $filePath 文件路径
* @param mixed $data 要保存的数据
* @param int $flags JSON编码标志默认JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
* @return bool 保存成功返回true失败返回false
*/
public static function saveJsonFile($filePath, $data, $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) {
// 确保目录存在
$dirPath = dirname($filePath);
if (!self::ensureDirExists($dirPath)) {
return false;
}
return file_put_contents($filePath, json_encode($data, $flags)) !== false;
}
/**
* 删除文件
* @param string $filePath 文件路径
* @return bool 删除成功返回true失败返回false
*/
public static function deleteFile($filePath) {
if (file_exists($filePath)) {
return unlink($filePath);
}
return true; // 文件不存在也视为删除成功
}
/**
* 获取文件修改时间
* @param string $filePath 文件路径
* @return int 文件修改时间戳文件不存在返回0
*/
public static function getFileMTime($filePath) {
if (file_exists($filePath)) {
return filemtime($filePath);
}
return 0;
}
}
?>