[PHP] 文件操作类 →→→→→进入此内容的聊天室

来自 , 2020-12-02, 写在 PHP, 查看 141 次.
URL http://www.code666.cn/view/db5cea26
  1. <?php
  2. /**
  3.  * 文件操作类
  4.  */
  5. !defined('PATH_ADMIN') && exit('Forbidden');
  6. class FileModel{
  7.     /**
  8.      * 写文件
  9.      *
  10.      * @param string $filename
  11.      * @param string $data
  12.      * @param string $method
  13.      * @param int $iflock
  14.      * @param int $check
  15.      * @param int $chmod
  16.      * @return boolean
  17.      */
  18.     public static function write($filename, $data, $method = 'wb+', $iflock = 1, $check = 1, $chmod = 1){
  19.         if(empty($filename)){
  20.             return false;
  21.         }
  22.  
  23.         if($check && strpos($filename,'..') !== false){
  24.             return false;
  25.         }
  26.  
  27.         if(!is_dir(dirname($filename)) && !self::mkdir_recursive(dirname($filename),0777)){
  28.             return false;
  29.         }
  30.         if(false == ($handle = fopen($filename,$method))){
  31.             return false;
  32.         }
  33.  
  34.         if($iflock){
  35.             flock($handle,LOCK_EX);
  36.         }
  37.         fwrite($handle,$data);
  38.         touch($filename);
  39.  
  40.         if($method == "wb+"){
  41.             ftruncate($handle,strlen($data));
  42.         }
  43.         fclose($handle);
  44.         $chmod && @chmod($filename,0777);
  45.  
  46.         return true;
  47.     }
  48.  
  49.     /**
  50.      * 读文件
  51.      *
  52.      * @param string $filename
  53.      * @param string $method
  54.      * @return string
  55.      */
  56.     public static function read($filename, $method = "rb"){
  57.         if(strpos($filename,'..') !== false){
  58.             return false;
  59.         }
  60.         if($handle = @fopen($filename,$method)){
  61.             flock($handle,LOCK_SH);
  62.             $filedata = @fread($handle,filesize($filename));
  63.             fclose($handle);
  64.             return $filedata;
  65.         }else{
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * 删除文件
  72.      *
  73.      * @param string $filename
  74.      * @return boolean
  75.      */
  76.     public static function rm($filename){
  77.         if(strpos($filename,'..') !== false){
  78.             return false;
  79.         }
  80.  
  81.         return @unlink($filename);
  82.     }
  83.  
  84.     /**
  85.      * 用递归方式创建目录
  86.      *
  87.      * @param string $pathname
  88.      * @param $mode
  89.      * @return boolean
  90.      */
  91.     public static function mkdir_recursive($pathname, $mode){
  92.         if(strpos($pathname,'..') !== false){
  93.             return false;
  94.         }
  95.         $pathname = rtrim(preg_replace(array(
  96.             '/\\{1,}/',
  97.             '/\/{2,}/'
  98.         ),'/',$pathname),'/');
  99.         if(is_dir($pathname)){
  100.             return true;
  101.         }
  102.  
  103.         is_dir(dirname($pathname)) || self::mkdir_recursive(dirname($pathname),$mode);
  104.         return is_dir($pathname) || @mkdir($pathname,$mode);
  105.     }
  106.  
  107.     /**
  108.      * 用递归方式删除目录
  109.      *
  110.      * @param string $file
  111.      * @return boolean
  112.      */
  113.     public static function rm_recurse($file){
  114.         if(strpos($file,'..') !== false){
  115.             return false;
  116.         }
  117.  
  118.         if(is_dir($file) && !is_link($file)){
  119.             foreach(scandir($file) as $sf){
  120.                 if($sf === '..' || $sf === '.'){
  121.                     continue;
  122.                 }
  123.                 if(!self::rm_recurse($file . '/' . $sf)){
  124.                     return false;
  125.                 }
  126.             }
  127.             return @rmdir($file);
  128.         }else{
  129.             return unlink($file);
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * 引用文件安全检查
  135.      *
  136.      * @param string $filename
  137.      * @param int $ifcheck
  138.      * @return boolean
  139.      */
  140.     public static function check_security($filename, $ifcheck = 1){
  141.         if(strpos($filename,'http://') !== false) return false;
  142.         if(strpos($filename,'https://') !== false) return false;
  143.         if(strpos($filename,'ftp://') !== false) return false;
  144.         if(strpos($filename,'ftps://') !== false) return false;
  145.         if(strpos($filename,'php://') !== false) return false;
  146.         if(strpos($filename,'..') !== false) return false;
  147.  
  148.         return $filename;
  149.     }
  150.  
  151.     /**
  152.      * 文件列表
  153.      *
  154.      * @param string $path //路径
  155.      * @param string $type[optional] //类型:file 文件,dir 目录, 缺省 file+dir
  156.      * @return array
  157.      */
  158.     public static function ls($path, $type = ''){
  159.         if(!is_dir($path)){
  160.             return false;
  161.         }
  162.         $files = scandir($path);
  163.         array_shift($files);
  164.         array_shift($files);
  165.         if(!empty($type) && in_array($type,array(
  166.             'file',
  167.             'dir'
  168.         ))){
  169.             $func = "is_" . $type;
  170.             foreach($files as $k => $cur_file){
  171.                 if(!$func($path . '/' . $cur_file) || $cur_file == '.svn'){
  172.                     unset($files[$k]);
  173.                 }
  174.             }
  175.         }
  176.         return $files;
  177.     }
  178. }
  179.  

回复 "文件操作类"

这儿你可以回复上面这条便签

captcha