123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: pengyong <i@pengyong.info>
- // +----------------------------------------------------------------------
- namespace crmeb\services;
- class FileService
- {
- /*
- @function 创建目录
- @var:$filename 目录名
- @return: true
- */
- static public function mk_dir($dir)
- {
- $dir = rtrim($dir, '/') . '/';
- if (!is_dir($dir)) {
- if (mkdir($dir, 0700) == false) {
- return false;
- }
- return true;
- }
- return true;
- }
- /*
- @function 写文件
- @var:$filename 文件名
- @var:$writetext 文件内容
- @var:$openmod 打开方式
- @return: 成功=true
- */
- static function write_file($filename, $writetext, $openmod = 'w')
- {
- if (!self::checkPath($filename)) {
- return false;
- }
- if (!self::checkContent($writetext)) {
- return false;
- }
- if (@$fp = fopen($filename, $openmod)) {
- flock($fp, 2);
- fwrite($fp, $writetext);
- fclose($fp);
- return true;
- } else {
- return false;
- }
- }
- /*
- @function 删除目录
- @var:$dirName 原目录
- @return: 成功=true
- */
- static function del_dir($dirName)
- {
- if (!file_exists($dirName)) {
- return false;
- }
- $dir = opendir($dirName);
- while ($fileName = readdir($dir)) {
- $file = $dirName . '/' . $fileName;
- if ($fileName != '.' && $fileName != '..') {
- if (is_dir($file)) {
- self::del_dir($file);
- } else {
- unlink($file);
- }
- }
- }
- closedir($dir);
- return rmdir($dirName);
- }
- /*
- @function 复制目录
- @var:$surDir 原目录
- @var:$toDir 目标目录
- @return: true
- */
- public function copy_dir($surDir, $toDir)
- {
- $surDir = rtrim($surDir, '/') . '/';
- $toDir = rtrim($toDir, '/') . '/';
- if (!file_exists($surDir)) {
- return false;
- }
- if (!file_exists($toDir)) {
- $this->create_dir($toDir);
- }
- $file = opendir($surDir);
- while ($fileName = readdir($file)) {
- $file1 = $surDir . '/' . $fileName;
- $file2 = $toDir . '/' . $fileName;
- if ($fileName != '.' && $fileName != '..') {
- if (is_dir($file1)) {
- self::copy_dir($file1, $file2);
- } else {
- copy($file1, $file2);
- }
- }
- }
- closedir($file);
- return true;
- }
- /*
- @function 列出目录
- @var:$dir 目录名
- @return: 目录数组
- 列出文件夹下内容,返回数组 $dirArray['dir']:存文件夹;$dirArray['file']:存文件
- */
- static function get_dirs($dir)
- {
- $dir = rtrim($dir, '/') . '/';
- $dirArray [][] = NULL;
- if (false != ($handle = opendir($dir))) {
- $i = 0;
- $j = 0;
- while (false !== ($file = readdir($handle))) {
- if (is_dir($dir . $file)) { //判断是否文件夹
- $dirArray ['dir'] [$i] = $file;
- $i++;
- } else {
- $dirArray ['file'] [$j] = $file;
- $j++;
- }
- }
- closedir($handle);
- }
- return $dirArray;
- }
- /*
- @function 统计文件夹大小
- @var:$dir 目录名
- @return: 文件夹大小(单位 B)
- */
- static function get_size($dir)
- {
- $dirlist = opendir($dir);
- $dirsize = 0;
- while (false !== ($folderorfile = readdir($dirlist))) {
- if ($folderorfile != "." && $folderorfile != "..") {
- if (is_dir("$dir/$folderorfile")) {
- $dirsize += self::get_size("$dir/$folderorfile");
- } else {
- $dirsize += filesize("$dir/$folderorfile");
- }
- }
- }
- closedir($dirlist);
- return $dirsize;
- }
- /*
- @function 检测是否为空文件夹
- @var:$dir 目录名
- @return: 存在则返回true
- */
- static function empty_dir($dir)
- {
- return (($files = @scandir($dir)) && count($files) <= 2);
- }
- /**
- * 创建多级目录
- * @param string $dir
- * @param int $mode
- * @return boolean
- */
- public function create_dir($dir, $mode = 0777)
- {
- return is_dir($dir) or ($this->create_dir(dirname($dir)) and mkdir($dir, $mode));
- }
- /**
- * 创建指定路径下的指定文件
- * @param string $path (需要包含文件名和后缀)
- * @param boolean $over_write 是否覆盖文件
- * @param int $time 设置时间。默认是当前系统时间
- * @param int $atime 设置访问时间。默认是当前系统时间
- * @return boolean
- */
- public function create_file($path, $over_write = FALSE, $time = NULL, $atime = NULL)
- {
- $path = $this->dir_replace($path);
- $time = empty($time) ? time() : $time;
- $atime = empty($atime) ? time() : $atime;
- if (file_exists($path) && $over_write) {
- $this->unlink_file($path);
- }
- $aimDir = dirname($path);
- $this->create_dir($aimDir);
- return touch($path, $time, $atime);
- }
- /**
- * 关闭文件操作
- * @param string $path
- * @return boolean
- */
- public function close($path)
- {
- return fclose($path);
- }
- /**
- * 读取文件操作
- * @param string $file
- * @return boolean
- */
- public static function read_file($file)
- {
- return @file_get_contents($file);
- }
- /**
- * 确定服务器的最大上传限制(字节数)
- * @return int 服务器允许的最大上传字节数
- */
- public function allow_upload_size()
- {
- $val = trim(ini_get('upload_max_filesize'));
- return $val;
- }
- /**
- * 字节格式化 把字节数格式为 B K M G T P E Z Y 描述的大小
- * @param int $size 大小
- * @param int $dec 显示类型
- * @return int
- */
- public static function byte_format($size, $dec = 2)
- {
- $a = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
- $pos = 0;
- while ($size >= 1024) {
- $size /= 1024;
- $pos++;
- }
- return round($size, $dec) . " " . $a[$pos];
- }
- /**
- * 删除非空目录
- * 说明:只能删除非系统和特定权限的文件,否则会出现错误
- * @param string $dirName 目录路径
- * @param boolean $is_all 是否删除所有
- * @param boolean $del_dir 是否删除目录
- * @return boolean
- */
- public function remove_dir($dir_path, $is_all = FALSE)
- {
- $dirName = $this->dir_replace($dir_path);
- $handle = @opendir($dirName);
- while (($file = @readdir($handle)) !== FALSE) {
- if ($file != '.' && $file != '..') {
- $dir = $dirName . '/' . $file;
- if ($is_all) {
- is_dir($dir) ? $this->remove_dir($dir) : $this->unlink_file($dir);
- } else {
- if (is_file($dir)) {
- $this->unlink_file($dir);
- }
- }
- }
- }
- closedir($handle);
- return @rmdir($dirName);
- }
- /**
- * 获取完整文件名
- * @param string $fn 路径
- * @return string
- */
- public function get_basename($file_path)
- {
- $file_path = $this->dir_replace($file_path);
- return basename(str_replace('\\', '/', $file_path));
- //return pathinfo($file_path,PATHINFO_BASENAME);
- }
- /**
- * 获取文件后缀名
- * @param string $file_name 文件路径
- * @return string
- */
- public static function get_ext($file)
- {
- $file = self::dir_replace($file);
- //return strtolower(substr(strrchr(basename($file), '.'),1));
- //return end(explode(".",$filename ));
- //return strtolower(trim(array_pop(explode('.', $file))));//取得后缀
- //return preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$file);
- return pathinfo($file, PATHINFO_EXTENSION);
- }
- /**
- * 取得指定目录名称
- * @param string $path 文件路径
- * @param int $num 需要返回以上级目录的数
- * @return string
- */
- public function father_dir($path, $num = 1)
- {
- $path = $this->dir_replace($path);
- $arr = explode('/', $path);
- if ($num == 0 || count($arr) < $num) return pathinfo($path, PATHINFO_BASENAME);
- return substr(strrev($path), 0, 1) == '/' ? $arr[(count($arr) - (1 + $num))] : $arr[(count($arr) - $num)];
- }
- /**
- * 删除文件
- * @param string $path
- * @return boolean
- */
- public function unlink_file($path)
- {
- $path = $this->dir_replace($path);
- if (file_exists($path)) {
- return unlink($path);
- }
- }
- /**
- * 文件操作(复制/移动)
- * @param string $old_path 指定要操作文件路径(需要含有文件名和后缀名)
- * @param string $new_path 指定新文件路径(需要新的文件名和后缀名)
- * @param string $type 文件操作类型
- * @param boolean $overWrite 是否覆盖已存在文件
- * @return boolean
- */
- public function handle_file($old_path, $new_path, $type = 'copy', $overWrite = FALSE)
- {
- $old_path = $this->dir_replace($old_path);
- $new_path = $this->dir_replace($new_path);
- if (file_exists($new_path) && $overWrite = FALSE) {
- return FALSE;
- } else if (file_exists($new_path) && $overWrite = TRUE) {
- $this->unlink_file($new_path);
- }
- $aimDir = dirname($new_path);
- $this->create_dir($aimDir);
- switch ($type) {
- case 'copy':
- return copy($old_path, $new_path);
- break;
- case 'move':
- return @rename($old_path, $new_path);
- break;
- }
- }
- /**
- * 文件夹操作(复制/移动)
- * @param string $old_path 指定要操作文件夹路径
- * @param string $aimDir 指定新文件夹路径
- * @param string $type 操作类型
- * @param boolean $overWrite 是否覆盖文件和文件夹
- * @return boolean
- */
- public function handle_dir($old_path, $new_path, $type = 'copy', $overWrite = FALSE)
- {
- $new_path = $this->check_path($new_path);
- $old_path = $this->check_path($old_path);
- if (!is_dir($old_path)) return FALSE;
- if (!file_exists($new_path)) $this->create_dir($new_path);
- $dirHandle = opendir($old_path);
- if (!$dirHandle) return FALSE;
- $boolean = TRUE;
- while (FALSE !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') continue;
- if (!is_dir($old_path . $file)) {
- $boolean = $this->handle_file($old_path . $file, $new_path . $file, $type, $overWrite);
- } else {
- $this->handle_dir($old_path . $file, $new_path . $file, $type, $overWrite);
- }
- }
- switch ($type) {
- case 'copy':
- closedir($dirHandle);
- return $boolean;
- break;
- case 'move':
- closedir($dirHandle);
- return @rmdir($old_path);
- break;
- }
- }
- /**
- * 替换相应的字符
- * @param string $path 路径
- * @return string
- */
- public static function dir_replace($path)
- {
- return str_replace('//', '/', str_replace('\\', '/', $path));
- }
- /**
- * 读取指定路径下模板文件
- * @param string $path 指定路径下的文件
- * @return string $rstr
- */
- public static function get_templtes($path)
- {
- $path = self::dir_replace($path);
- if (file_exists($path)) {
- $fp = fopen($path, 'r');
- $rstr = fread($fp, filesize($path));
- fclose($fp);
- return $rstr;
- } else {
- return '';
- }
- }
- /**
- * 文件重命名
- * @param string $oldname
- * @param string $newname
- */
- public function rename($oldname, $newname)
- {
- if (($newname != $oldname) && is_writable($oldname)) {
- return rename($oldname, $newname);
- }
- }
- /**
- * 获取指定路径下的信息
- * @param string $dir 路径
- * @return ArrayObject
- */
- public function get_dir_info($dir)
- {
- $handle = @opendir($dir);//打开指定目录
- $directory_count = 0;
- $total_size = 0;
- $file_cout = 0;
- while (FALSE !== ($file_path = readdir($handle))) {
- if ($file_path != "." && $file_path != "..") {
- //is_dir("$dir/$file_path") ? $sizeResult += $this->get_dir_size("$dir/$file_path") : $sizeResult += filesize("$dir/$file_path");
- $next_path = $dir . '/' . $file_path;
- if (is_dir($next_path)) {
- $directory_count++;
- $result_value = self::get_dir_info($next_path);
- $total_size += $result_value['size'];
- $file_cout += $result_value['filecount'];
- $directory_count += $result_value['dircount'];
- } elseif (is_file($next_path)) {
- $total_size += filesize($next_path);
- $file_cout++;
- }
- }
- }
- closedir($handle);//关闭指定目录
- $result_value['size'] = $total_size;
- $result_value['filecount'] = $file_cout;
- $result_value['dircount'] = $directory_count;
- return $result_value;
- }
- /**
- * 指定文件编码转换
- * @param string $path 文件路径
- * @param string $input_code 原始编码
- * @param string $out_code 输出编码
- * @return boolean
- */
- public function change_file_code($path, $input_code, $out_code)
- {
- if (is_file($path))//检查文件是否存在,如果存在就执行转码,返回真
- {
- $content = file_get_contents($path);
- $content = string::chang_code($content, $input_code, $out_code);
- $fp = fopen($path, 'w');
- return fputs($fp, $content) ? TRUE : FALSE;
- fclose($fp);
- }
- }
- /**
- * 指定目录下指定条件文件编码转换
- * @param string $dirname 目录路径
- * @param string $input_code 原始编码
- * @param string $out_code 输出编码
- * @param boolean $is_all 是否转换所有子目录下文件编码
- * @param string $exts 文件类型
- * @return boolean
- */
- public function change_dir_files_code($dirname, $input_code, $out_code, $is_all = TRUE, $exts = '')
- {
- if (is_dir($dirname)) {
- $fh = opendir($dirname);
- while (($file = readdir($fh)) !== FALSE) {
- if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) {
- continue;
- }
- $filepath = $dirname . '/' . $file;
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = $this->change_dir_files_code($filepath, $input_code, $out_code, $is_all, $exts);
- } else {
- if ($this->get_ext($filepath) == $exts && is_file($filepath)) {
- $boole = $this->change_file_code($filepath, $input_code, $out_code, $is_all, $exts);
- if (!$boole) continue;
- }
- }
- }
- closedir($fh);
- return TRUE;
- } else {
- return FALSE;
- }
- }
- /**
- * 列出指定目录下符合条件的文件和文件夹
- * @param string $dirname 路径
- * @param boolean $is_all 是否列出子目录中的文件
- * @param string $exts 需要列出的后缀名文件
- * @param string $sort 数组排序
- * @return ArrayObject
- */
- public function list_dir_info($dirname, $is_all = FALSE, $exts = '', $sort = 'ASC')
- {
- //处理多于的/号
- $new = strrev($dirname);
- if (strpos($new, '/') == 0) {
- $new = substr($new, 1);
- }
- $dirname = strrev($new);
- $sort = strtolower($sort);//将字符转换成小写
- $files = array();
- $subfiles = array();
- if (is_dir($dirname)) {
- $fh = opendir($dirname);
- while (($file = readdir($fh)) !== FALSE) {
- if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) continue;
- $filepath = $dirname . '/' . $file;
- switch ($exts) {
- case '*':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
- }
- array_push($files, $filepath);
- break;
- case 'folder':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
- array_push($files, $filepath);
- } elseif (is_dir($filepath)) {
- array_push($files, $filepath);
- }
- break;
- case 'file':
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
- } elseif (is_file($filepath)) {
- array_push($files, $filepath);
- }
- break;
- default:
- if (is_dir($filepath) && $is_all == TRUE) {
- $files = array_merge($files, self::list_dir_info($filepath, $is_all, $exts, $sort));
- } elseif (preg_match("/\.($exts)/i", $filepath) && is_file($filepath)) {
- array_push($files, $filepath);
- }
- break;
- }
- switch ($sort) {
- case 'asc':
- sort($files);
- break;
- case 'desc':
- rsort($files);
- break;
- case 'nat':
- natcasesort($files);
- break;
- }
- }
- closedir($fh);
- return $files;
- } else {
- return FALSE;
- }
- }
- /**
- * 返回指定路径的文件夹信息,其中包含指定路径中的文件和目录
- * @param string $dir
- * @return ArrayObject
- */
- public function dir_info($dir)
- {
- return scandir($dir);
- }
- /**
- * 判断目录是否为空
- * @param string $dir
- * @return boolean
- */
- public function is_empty($dir)
- {
- $handle = opendir($dir);
- while (($file = readdir($handle)) !== false) {
- if ($file != '.' && $file != '..') {
- closedir($handle);
- return true;
- }
- }
- closedir($handle);
- return false;
- }
- /**
- * 返回指定文件和目录的信息
- * @param string $file
- * @return ArrayObject
- */
- public static function list_info($file)
- {
- $dir = array();
- $dir['filename'] = basename($file);//返回路径中的文件名部分。
- $dir['pathname'] = strstr(php_uname('s'), 'Windows') ? str_replace('\\', '\\\\', realpath($file)) : realpath($file);//返回绝对路径名。
- $dir['owner'] = fileowner($file);//文件的 user ID (所有者)。
- $dir['perms'] = fileperms($file);//返回文件的 inode 编号。
- $dir['inode'] = fileinode($file);//返回文件的 inode 编号。
- $dir['group'] = filegroup($file);//返回文件的组 ID。
- $dir['path'] = dirname($file);//返回路径中的目录名称部分。
- $dir['atime'] = fileatime($file);//返回文件的上次访问时间。
- $dir['ctime'] = filectime($file);//返回文件的上次改变时间。
- $dir['perms'] = fileperms($file);//返回文件的权限。
- $dir['size'] = self::byte_format(filesize($file), 2);//返回文件大小。
- $dir['type'] = filetype($file);//返回文件类型。
- $dir['ext'] = is_file($file) ? pathinfo($file, PATHINFO_EXTENSION) : '';//返回文件后缀名
- $dir['mtime'] = filemtime($file);//返回文件的上次修改时间。
- $dir['isDir'] = is_dir($file);//判断指定的文件名是否是一个目录。
- $dir['isFile'] = is_file($file);//判断指定文件是否为常规的文件。
- $dir['isLink'] = is_link($file);//判断指定的文件是否是连接。
- $dir['isReadable'] = is_readable($file);//判断文件是否可读。
- $dir['isWritable'] = is_writable($file);//判断文件是否可写。
- $dir['isUpload'] = is_uploaded_file($file);//判断文件是否是通过 HTTP POST 上传的。
- return $dir;
- }
- /**
- * 返回关于打开文件的信息
- * @param $file
- * @return ArrayObject
- * 数字下标 关联键名(自 PHP 4.0.6) 说明
- * 0 dev 设备名
- * 1 ino 号码
- * 2 mode inode 保护模式
- * 3 nlink 被连接数目
- * 4 uid 所有者的用户 id
- * 5 gid 所有者的组 id
- * 6 rdev 设备类型,如果是 inode 设备的话
- * 7 size 文件大小的字节数
- * 8 atime 上次访问时间(Unix 时间戳)
- * 9 mtime 上次修改时间(Unix 时间戳)
- * 10 ctime 上次改变时间(Unix 时间戳)
- * 11 blksize 文件系统 IO 的块大小
- * 12 blocks 所占据块的数目
- */
- public function open_info($file)
- {
- $file = fopen($file, "r");
- $result = fstat($file);
- fclose($file);
- return $result;
- }
- /**
- * 改变文件和目录的相关属性
- * @param string $file 文件路径
- * @param string $type 操作类型
- * @param string $ch_info 操作信息
- * @return boolean
- */
- public function change_file($file, $type, $ch_info)
- {
- switch ($type) {
- case 'group' :
- $is_ok = chgrp($file, $ch_info);//改变文件组。
- break;
- case 'mode' :
- $is_ok = chmod($file, $ch_info);//改变文件模式。
- break;
- case 'ower' :
- $is_ok = chown($file, $ch_info);//改变文件所有者。
- break;
- }
- }
- /**
- * 取得文件路径信息
- * @param $full_path 完整路径
- * @return ArrayObject
- */
- public function get_file_type($path)
- {
- //pathinfo() 函数以数组的形式返回文件路径的信息。
- //---------$file_info = pathinfo($path); echo file_info['extension'];----------//
- //extension取得文件后缀名【pathinfo($path,PATHINFO_EXTENSION)】-----dirname取得文件路径【pathinfo($path,PATHINFO_DIRNAME)】-----basename取得文件完整文件名【pathinfo($path,PATHINFO_BASENAME)】-----filename取得文件名【pathinfo($path,PATHINFO_FILENAME)】
- return pathinfo($path);
- }
- /**
- * 取得上传文件信息
- * @param $file file属性信息
- * @return array
- */
- public function get_upload_file_info($file)
- {
- $file_info = $_FILES[$file];//取得上传文件基本信息
- $info = array();
- $info['type'] = strtolower(trim(stripslashes(preg_replace("/^(.+?);.*$/", "\\1", $file_info['type'])), '"'));//取得文件类型
- $info['temp'] = $file_info['tmp_name'];//取得上传文件在服务器中临时保存目录
- $info['size'] = $file_info['size'];//取得上传文件大小
- $info['error'] = $file_info['error'];//取得文件上传错误
- $info['name'] = $file_info['name'];//取得上传文件名
- $info['ext'] = $this->get_ext($file_info['name']);//取得上传文件后缀
- return $info;
- }
- /**
- * 设置文件命名规则
- * @param string $type 命名规则
- * @param string $filename 文件名
- * @return string
- */
- public function set_file_name($type)
- {
- switch ($type) {
- case 'hash' :
- $new_file = md5(uniqid(mt_rand()));//mt_srand()以随机数md5加密来命名
- break;
- case 'time' :
- $new_file = time();
- break;
- default :
- $new_file = date($type, time());//以时间格式来命名
- break;
- }
- return $new_file;
- }
- /**
- * 文件保存路径处理
- * @return string
- */
- public function check_path($path)
- {
- return (preg_match('/\/$/', $path)) ? $path : $path . '/';
- }
- /**
- * 文件下载
- * $save_dir 保存路径
- * $filename 文件名
- * @return array
- */
- public static function down_remote_file($url, $save_dir = '', $filename = '', $type = 0)
- {
- if (trim($url) == '') {
- return array('file_name' => '', 'save_path' => '', 'error' => 1);
- }
- if (trim($save_dir) == '') {
- $save_dir = './';
- }
- if (trim($filename) == '') {//保存文件名
- $ext = strrchr($url, '.');
- // if($ext!='.gif'&&$ext!='.jpg'){
- // return array('file_name'=>'','save_path'=>'','error'=>3);
- // }
- $filename = time() . $ext;
- }
- if (0 !== strrpos($save_dir, '/')) {
- $save_dir .= '/';
- }
- //创建保存目录
- if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
- return array('file_name' => '', 'save_path' => '', 'error' => 5);
- }
- //获取远程文件所采用的方法
- if ($type) {
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $img = curl_exec($ch);
- curl_close($ch);
- } else {
- ob_start();
- readfile($url);
- $img = ob_get_contents();
- ob_end_clean();
- }
- //$size=strlen($img);
- //文件大小
- $fp2 = fopen($save_dir . $filename, 'a');
- fwrite($fp2, $img);
- fclose($fp2);
- unset($img, $url);
- return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);
- }
- public static function zipopen($filename, $savename)
- {
- $zip = new \ZipArchive;
- $zipfile = $filename;
- $res = $zip->open($zipfile);
- $toDir = $savename;
- if (!file_exists($toDir)) mkdir($toDir, 0777);
- $docnum = $zip->numFiles;
- for ($i = 0; $i < $docnum; $i++) {
- $statInfo = $zip->statIndex($i);
- if ($statInfo['crc'] == 0 && $statInfo['comp_size'] != 2) {
- //新建目录
- mkdir($toDir . '/' . substr($statInfo['name'], 0, -1), 0777);
- } else {
- //拷贝文件
- copy('zip://' . $zipfile . '#' . $statInfo['name'], $toDir . '/' . $statInfo['name']);
- }
- }
- $zip->close();
- return true;
- }
- /**
- *设置字体格式
- * @param $title string 必选
- * return string
- */
- public static function setUtf8($title)
- {
- return iconv('utf-8', 'gb2312', $title);
- }
- /**
- *检查指定文件是否能写入
- * @param $file string 必选
- * return boole
- */
- public static function isWritable($file)
- {
- $file = str_replace('\\', '/', $file);
- if (!file_exists($file)) return false;
- return is_writable($file);
- }
- /**
- * 验证目录结构中是否包含特殊字符
- * @param $path
- * @return bool
- */
- public static function checkPath($path)
- {
- $str = ['/%00/', '"/\/|\~|\,|\。|\!|\?|\“|\”|\【|\】|\『|\』|\:|\;|\《|\》|\’|\‘|\ |\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\/|\;|\'|\`|\=|\\\|\|/"'];
- foreach ($str as $value) {
- if (preg_match($value, $path)) {
- return false;
- }
- }
- return true;
- }
- /**
- * 验证文件内容中可能存在的shell
- * @param $content
- * @return bool
- */
- public static function checkContent($content)
- {
- //加密类型shell
- if ((preg_match('#(\$\w{2,4}\s?=\s?str_replace\("\w+","","[\w_]+"\);\s?)+#s', $content) && preg_match('#(\$\w{2,4}\s?=\s?"[\w\d\+\/\=]+";\s?)+#', $content) && preg_match('#\$[\w]{2,4}\s?=\s\$[\w]{2,4}\(\'\',\s?\$\w{2,4}\(\$\w{2,4}\("\w{1,4}",\s?"",\s?\$\w{2,4}\.\$\w{2,4}\.\$\w{2,4}\.\$\w{2,4}\)\)\);\s+?\$\w{2,4}\(\)\;#', $content))
- ||
- (preg_match('#\$\w+\d\s?=\s?str_replace\(\"[\w\d]+\",\"\",\"[\w\d]+\"\);#s', $content) && preg_match('#\$\w+\s?=\s?\$[\w\d]+\(\'\',\s?\$[\w\d]+\(\$\w+\(\$\w+\(\"[[:punct:]]+\",\s?\"\",\s?\$\w+\.\$\w+\.\$\w+\.\$\w+\)\)\)\);\s?\$\w+\(\);#s', $content))
- ) {
- return false;
- }
- //回调类型
- if (preg_match('#\$\w+\s?=\s?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]#is', $content) &&
- preg_match('#\$\w+\s?=\s?(?:new)?\s?array\w*\s?\(.*?_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\].*?\)+#is', $content) &&
- preg_match('#(?:array_(?:reduce|map|udiff|walk|walk_recursive|filter)|u[ak]sort)\s?\(.*?\)+?#is', $content)
- ) {
- return false;
- }
- //内容过滤
- $matches = [
- '/mb_ereg_replace\([\'\*\s\,\.\"]+\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'\"].*?[\'\"][\]][\,\s\'\"]+e[\'\"]/is',
- '/preg_filter\([\'\"\|\.\*e]+.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)/is',
- '/create_function\s?\(.*assert\(/is',
- '/ini_get\(\'safe_mode\'\)/i',
- '/get_current_user\(.*?\)/i',
- '/@?assert\s?\(\$.*?\)/i',
- '/proc_open\s?\(.*?pipe\',\s?\'w\'\)/is',
- '/sTr_RepLaCe\s?\([\'\"].*?[\'\"],[\'\"].*?[\'\"]\s?,\s?\'a[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?e[[:alnum:][:punct:]]+?r[[:alnum:][:punct:]]+?t[[:alnum:][:punct:]]+?\)/i',
- '/preg_replace_callback\(.*?create_function\(/is',
- '/filter_var(?:_array)?\s?.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'\"][[:punct:][:alnum:]]+[\'\"]\][[:punct:][:alnum:][:space:]]+?assert[\'\"]\)/is',
- '/ob_start\([\'\"]+assert[\'\"]+\)/is',
- '/new\s?ReflectionFunction\(.*?->invoke\(/is',
- '/PDO::FETCH_FUNC/',
- '/\$\w+.*\s?(?:=|->)\s?.*?[\'\"]assert[\'\"]\)?/i',
- '/\$\w+->(?:sqlite)?createFunction\(.*?\)/i',
- '/eval\([\"\']?\\\?\$\w+\s?=\s?.*?\)/i',
- '/eval\(.*?gzinflate\(base64_decode\(/i',
- '/copy\(\$HTTP_POST_FILES\[\'\w+\'\]\s?\[\'tmp_name\'\]/i',
- '/register_(?:shutdown|tick)_function\s?\(\$\w+,\s\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]\)/is',
- '/register_(?:shutdown|tick)_function\s?\(?[\'\"]assert[\"\'].*?\)/i',
- '/call_user_func.*?\([\"|\']assert[\"|\'],.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'|\"].*\]\)+/is',
- '/preg_replace\(.*?e.*?\'\s?,\s?.*?\w+\(.*?\)/i',
- '/function_exists\s*\(\s*[\'|\"](popen|exec|proc_open|system|passthru)+[\'|\"]\s*\)/i',
- '/(exec|shell_exec|system|passthru)+\s*\(\s*\$_(\w+)\[(.*)\]\s*\)/i',
- '/(exec|shell_exec|system|passthru)+\s*\(\$\w+\)/i',
- '/(exec|shell_exec|system|passthru)\s?\(\w+\(\"http_.*\"\)\)/i',
- '/(?:john\.barker446@gmail\.com|xb5@hotmail\.com|shopen@aventgrup\.net|milw0rm\.com|www\.aventgrup\.net|mgeisler@mgeisler\.net)/i',
- '/Php\s*?Shell/i',
- '/((udp|tcp)\:\/\/(.*)\;)+/i',
- '/preg_replace\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i',
- '/preg_replace\s*\((.*)\(base64_decode\(\$/i',
- '/(eval|assert|include|require|include_once|require_once)+\s*\(\s*(base64_decode|str_rot13|gz(\w+)|file_(\w+)_contents|(.*)php\:\/\/input)+/i',
- '/(eval|assert|include|require|include_once|require_once|array_map|array_walk)+\s*\(.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER|SESSION)+\[(.*)\]\s*\)/i',
- '/eval\s*\(\s*\(\s*\$\$(\w+)/i',
- '/((?:include|require|include_once|require_once)+\s*\(?\s*[\'|\"]\w+\.(?!php).*[\'|\"])/i',
- '/\$_(\w+)(.*)(eval|assert|include|require|include_once|require_once)+\s*\(\s*\$(\w+)\s*\)/i',
- '/\(\s*\$_FILES\[(.*)\]\[(.*)\]\s*\,\s*\$_(GET|POST|REQUEST|FILES)+\[(.*)\]\[(.*)\]\s*\)/i',
- '/(fopen|fwrite|fputs|file_put_contents)+\s*\((.*)\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\](.*)\)/i',
- '/echo\s*curl_exec\s*\(\s*\$(\w+)\s*\)/i',
- '/new com\s*\(\s*[\'|\"]shell(.*)[\'|\"]\s*\)/i',
- '/\$(.*)\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i',
- '/\$_\=(.*)\$_/i',
- '/\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\(\s*\$(.*)\)/i',
- '/\$(\w+)\s*\(\s*\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\s*\)/i',
- '/\$(\w+)\s*\(\s*\$\{(.*)\}/i',
- '/\$(\w+)\s*\(\s*chr\(\d+\)/i'
- ];
- foreach ($matches as $value) {
- if (preg_match($value, $content)) {
- return false;
- }
- }
- return true;
- }
- }
|