253 lines
5.5 KiB
PHP
Executable File
253 lines
5.5 KiB
PHP
Executable File
<?php
|
||
/**
|
||
* @author Fm453
|
||
* @site www.hiluker.com
|
||
* @remark:文件处理
|
||
*/
|
||
defined('HI_FM') or exit("Access Denied");
|
||
global $_GPC;
|
||
global $_HI;
|
||
global $_FM;
|
||
fm_load()->fm_func('fm');
|
||
|
||
$_HI['setting']['filemode'] = 0777; //该数字不可加引号
|
||
//判断文件是否存在
|
||
/*
|
||
@filename 文件名
|
||
@route 相对于yii安装根目录的路径
|
||
*/
|
||
|
||
function fmFunc_file_checkExist($filename,$route)
|
||
{
|
||
global $_FM;
|
||
$file = $route.$filename;
|
||
$file = FM_YII_ROOT.$file;
|
||
|
||
if(file_exists($file)){
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//创建目录
|
||
/*
|
||
@path 文件目录
|
||
*/
|
||
function fmFunc_file_mkdirs($path)
|
||
{
|
||
if (!is_dir($path)) {
|
||
fmFunc_file_mkdirs(dirname($path));
|
||
mkdir($path);
|
||
}
|
||
return is_dir($path);
|
||
}
|
||
|
||
//写文件
|
||
/*
|
||
@filename 文件名
|
||
@data 要写入的数据
|
||
*/
|
||
|
||
function fmFunc_file_write($filename,$data)
|
||
{
|
||
global $_GPC;
|
||
global $_HI;
|
||
global $_FM;
|
||
fmFunc_file_mkdirs(dirname($filename));
|
||
file_put_contents($filename, $data);
|
||
@chmod($filename, $_HI['setting']['filemode']);
|
||
return is_file($filename);
|
||
}
|
||
|
||
//读文件
|
||
/*
|
||
@filename 完整文件名
|
||
*/
|
||
|
||
function fmFunc_file_read($filename)
|
||
{
|
||
global $_FM;
|
||
$filename = FM_YII_ROOT.$filename;
|
||
if (!is_file($filename)) {
|
||
return false;
|
||
}
|
||
return file_get_contents($filename);
|
||
}
|
||
|
||
//移动文件
|
||
/*
|
||
@filename 完整文件名
|
||
@destination 要移入的目标路径
|
||
*/
|
||
|
||
function fmFunc_file_move($filename,$destination)
|
||
{
|
||
global $_GPC;
|
||
global $_HI;
|
||
global $_FM;
|
||
$filename = FM_YII_ROOT.$filename;
|
||
fmFunc_file_mkdirs(dirname($dest));
|
||
if (is_uploaded_file($filename)) {
|
||
move_uploaded_file($filename, $dest);
|
||
} else {
|
||
rename($filename, $destination);
|
||
}
|
||
@chmod($filename, $_HI['setting']['filemode']);
|
||
return is_file($destination);
|
||
}
|
||
|
||
//复制文件
|
||
/*
|
||
@src 源文件(要被复制的文件)
|
||
@des 目标文件
|
||
@filter 过滤器
|
||
*/
|
||
|
||
function fmFunc_file_copy($src, $des, $filter)
|
||
{
|
||
global $_FM;
|
||
$dir = opendir($src);
|
||
@mkdir($des);
|
||
while (false !== ($file = readdir($dir))) {
|
||
if (($file != '.') && ($file != '..')) {
|
||
if (is_dir($src . '/' . $file)) {
|
||
fmFunc_file_copy($src . '/' . $file, $des . '/' . $file, $filter);
|
||
} elseif (!in_array(substr($file, strrpos($file, '.') + 1), $filter)) {
|
||
copy($src . '/' . $file, $des . '/' . $file);
|
||
}
|
||
}
|
||
}
|
||
closedir($dir);
|
||
}
|
||
|
||
//删除目录
|
||
/*
|
||
@path 文件目录
|
||
@clean 是否清空
|
||
*/
|
||
function fmFunc_file_rmdirs($path, $clean = false)
|
||
{
|
||
if (!is_dir($path)) {
|
||
return false;
|
||
}
|
||
$files = glob($path . '/*');
|
||
if ($files) {
|
||
foreach ($files as $file) {
|
||
is_dir($file) ? fmFunc_file_rmdirs($file) : @unlink($file);
|
||
}
|
||
}
|
||
return $clean ? true : @fmFunc_file_rmdir($path);
|
||
}
|
||
|
||
//删除指定文件(永久删除)
|
||
/*
|
||
@file 文件名(含路径)
|
||
*/
|
||
|
||
function fmFunc_file_delete($file) {
|
||
if (empty($file)) {
|
||
return FALSE;
|
||
}
|
||
if (file_exists($file)) {
|
||
@unlink($file);
|
||
}
|
||
if (file_exists(FM_YII_ROOT . '/' . $file)) {
|
||
@unlink(FM_YII_ROOT . '/' . $file);
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
//列出目录树
|
||
/*
|
||
@path 文件目录
|
||
@include 文件名中包含的某些特定元素
|
||
*/
|
||
function fmFunc_file_tree($path, $include = array()) {
|
||
$files = array();
|
||
if (!empty($include)) {
|
||
$ds = glob($path . '/{' . implode(',', $include) . '}', GLOB_BRACE);
|
||
} else {
|
||
$ds = glob($path . '/*');
|
||
}
|
||
if (is_array($ds)) {
|
||
foreach ($ds as $entry) {
|
||
if (is_file($entry)) {
|
||
$files[] = $entry;
|
||
}
|
||
if (is_dir($entry)) {
|
||
$rs = fmFunc_file_tree($entry);
|
||
foreach ($rs as $f) {
|
||
$files[] = $f;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return $files;
|
||
}
|
||
|
||
//文件随机命名
|
||
/*
|
||
@dir 文件目录
|
||
@ext 文件扩展名
|
||
*/
|
||
function fmFunc_file_randomName($dir, $ext) {
|
||
do {
|
||
$filename = random(32) . '.' . $ext;
|
||
} while (file_exists($dir . $filename));
|
||
|
||
return $filename;
|
||
}
|
||
|
||
/*______________________________________数据文件类操作______________________________*/
|
||
//写数据文件(data目录)
|
||
/*
|
||
@filename PHP文件名
|
||
@route 相对于数据根目录data的路径
|
||
@data 要写入的数据
|
||
*/
|
||
|
||
function fmFunc_file_dataWrite($filename,$data,$uri=null,$origin_cache_key=null)
|
||
{
|
||
global $_FM;
|
||
$filelink = 'data/';
|
||
$file = $filelink.$filename;
|
||
$file = FM_YII_ROOT.$file;
|
||
$file = $file.'.php';
|
||
// 在数据目录中添加一个文件并写入内容信息
|
||
$content = "<?php";
|
||
$content .= "\r\n";
|
||
$content .= "/*";
|
||
$content .= "\r\n";
|
||
|
||
if($uri) {
|
||
$content .= "* @url 请求源:".$uri;
|
||
$content .= "\r\n";
|
||
}
|
||
if($origin_cache_key) {
|
||
$content .= "* @key 关联键:".$origin_cache_key;
|
||
$content .= "\r\n";
|
||
$content .= "* @checkmd5 文件名校验:".md5($origin_cache_key);
|
||
$content .= "\r\n";
|
||
}
|
||
|
||
$content .= "*/";
|
||
$content .= "\r\n";
|
||
$content .= "\$FmApiDataCache = '';";
|
||
$content .= "\r\n";
|
||
$content .= "\$FmApiDataCache = ";
|
||
$content .= "'".$data."'"; //写入前已经进行序列化操作,以便能存储各种类型的数据
|
||
$content .= ";";
|
||
|
||
fmFunc_file_write($file,$content);
|
||
}
|
||
|
||
function fmFunc_file_dataLoad($filename)
|
||
{
|
||
global $_FM;
|
||
$filelink = 'data/';
|
||
$file = $filelink.$filename;
|
||
$file = FM_YII_ROOT.$file;
|
||
require $file;;
|
||
$data = htmlspecialchars(iunserializer($FmApiDataCache));
|
||
return $data;
|
||
} |