Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72048
  • 博文数量: 67
  • 博客积分: 1334
  • 博客等级: 中尉
  • 技术积分: 670
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 10:56
文章分类

全部博文(67)

文章存档

2016年(2)

2015年(2)

2012年(9)

2011年(54)

我的朋友
最近访客

分类: 系统运维

2011-12-21 16:48:12

  1. <?php
  2. /**
  3.  * clearstatcache() 函数清除文件状态缓存。
  4.    clearstatcache() 函数会缓存某些函数的返回信息,以便提供更高的性能。
  5.    但是有时候,比如在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存,以便获得正确的结果。
  6.    要做到这一点,就需要使用 clearstatcache() 函数。
  7.    
  8.    会进行缓存的函数,即受 clearstatcache() 函数影响的函数:
  9.     stat()
  10.     lstat()
  11.     file_exists()
  12.     is_writable()
  13.     is_readable()
  14.     is_executable()
  15.     is_file()
  16.     is_dir()
  17.     is_link()
  18.     filectime()
  19.     fileatime()
  20.     filemtime()
  21.     fileinode()
  22.     filegroup()
  23.     fileowner()
  24.     filesize()
  25.     filetype()
  26.     fileperms()

  27.  *
  28.  * Enter description here ...
  29.  * @author maofeng
  30.  *
  31.  */
  32. class Mf_Helpers_File extends Zend_Controller_Action_Helper_Abstract
  33. {
  34.     
  35.     public function __construct(){
  36.     
  37.     }
  38.     
  39.     /**
  40.      * file_get_contents()
  41.      *
  42.      * Enter description here ...
  43.      * @param unknown_type $fileInfo 包含文的路径信息,例如 /home/wwwroot/
  44.      * $fileInfo = array('public', 'attachment', 'demo.txt');
  45.      */
  46.     public function read($fileInfo = array()){
  47.         $result = '';
  48.         
  49.         if(!empty($fileInfo)){    
  50.             $str = '';
  51.             foreach ($fileInfo as $v){
  52.                 $str .= '/'.$v;
  53.             }
  54.             $str = trim($str, '/');
  55.         }
  56.         
  57.         $realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
  58.         $attachmentPath = $realPath.$str;
  59.         
  60.         if(file_exists($attachmentPath)){
  61.             $result = file_get_contents($attachmentPath);
  62.         }else{
  63.             //echo '文件不存在';exit;
  64.             return false;
  65.         }

  66.         return $result;
  67.     }
  68.     
  69.     /**
  70.      * file_put_contents()
  71.      * Enter description here ...
  72.      * @param unknown_type $fileInfo
  73.      * @param unknown_type $content
  74.      * $fileInfo = array('public', 'attachment', 'demo.txt');
  75.      */
  76.     public function write($fileInfo = array(), $content = ''){
  77.     
  78.         if(!empty($fileInfo)){    
  79.             $str = '';
  80.             foreach ($fileInfo as $v){
  81.                 $str .= '/'.$v;
  82.             }
  83.             $str = trim($str, '/');
  84.         }
  85.         
  86.         $realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
  87.         $attachmentPath = $realPath.$str;
  88.         
  89.         $arr = explode('/', trim($attachmentPath, '/'));
  90.         
  91.         $dir = '';
  92.         for($i=0; $i<(count($arr)-1); $i++){
  93.             $dir .= '/'.$arr[$i];
  94.         }

  95.         if(is_dir($dir) && is_writable($dir)){
  96.             $byte = file_put_contents($attachmentPath, $content);
  97.             if($byte > 0){
  98.                 return true;
  99.             }else{
  100.                 return false;
  101.             }
  102.         }else{    
  103.             return false;
  104.         }

  105.     }

  106.     /**
  107.      *
  108.      *
  109.      * Enter description here ...
  110.      * @param unknown_type $fileName 要上传的文件源信息
  111.      * @param unknown_type $fileInfo 上传目标文件的信息 array('public', 'attachment') 代表/public/attachment/目录下
  112.      * @param unknown_type $newFileName 新上传文件的名字,一般会重命名,即重命名 default = 'newName' ,
  113.      * @param unknown_type $limit 允许上传的文件的后辍限制
  114.      * $fileInfo = array('public', 'attachment', 'demo.txt');
  115.      */
  116.     public function upload($fileName = '', $pathInfo = array(), $newFileName = 'newName', $limit = array('tailer'=>array('txt'), 'size'=>102400 )){

  117.         if(is_uploaded_file($_FILES[$fileName]['tmp_name'])){
  118.             
  119.             if(!empty($pathInfo)){    
  120.                 $str = '';
  121.                 foreach ($pathInfo as $v){
  122.                     $str .= '/'.$v;
  123.                 }
  124.                 $str = trim($str, '/');
  125.             }
  126.             
  127.             $realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
  128.             $dir = $realPath.$str;
  129.         
  130.             if(is_dir($dir) && is_writable($dir)){
  131.                 
  132.                 $oldName = $_FILES[$fileName]['name'];
  133.                 
  134.                 $cacheArray = explode('.', $oldName);
  135.                 
  136.                 $tailer = $cacheArray[count($cacheArray) - 1];
  137.                 
  138.                 if(in_array($tailer, $limit['tailer'])){
  139.                 
  140.                     $newName = $newFileName.'.'.$tailer;
  141.                     
  142.                     $destination = $dir.'/'.$newName;
  143.     
  144.                     move_uploaded_file($_FILES[$fileName]['tmp_name'], $destination);
  145.                     
  146.                 }
  147.                 
  148.             }
  149.         
  150.         }
  151.     }
  152.     
  153.     
  154.     public function download($fileInfo = array(), $newName = 'newFile.txt'){
  155.     
  156.         
  157.         $result = '';
  158.         
  159.         if(!empty($fileInfo)){    
  160.             $str = '';
  161.             foreach ($fileInfo as $v){
  162.                 $str .= '/'.$v;
  163.             }
  164.             $str = trim($str, '/');
  165.         }
  166.         
  167.         $realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
  168.         $attachmentPath = $realPath.$str;
  169.         
  170.         if(file_exists($attachmentPath)){
  171.             
  172.             header("Content-Type:application/octet-stream");
  173.             header("Content-Disposition: attachment; filename=$newName");
  174.             
  175.             header("Pragma: no-cache");
  176.             header("Expires: 0");
  177.             echo file_get_contents($attachmentPath);
  178.             exit;
  179.             
  180.         }
  181.     
  182.     }
  183.     
  184.     
  185.     
  186.     
  187. }
阅读(1205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~