- <?php
-
/**
-
* clearstatcache() 函数清除文件状态缓存。
-
clearstatcache() 函数会缓存某些函数的返回信息,以便提供更高的性能。
-
但是有时候,比如在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存,以便获得正确的结果。
-
要做到这一点,就需要使用 clearstatcache() 函数。
-
-
会进行缓存的函数,即受 clearstatcache() 函数影响的函数:
-
stat()
-
lstat()
-
file_exists()
-
is_writable()
-
is_readable()
-
is_executable()
-
is_file()
-
is_dir()
-
is_link()
-
filectime()
-
fileatime()
-
filemtime()
-
fileinode()
-
filegroup()
-
fileowner()
-
filesize()
-
filetype()
-
fileperms()
-
-
*
-
* Enter description here ...
-
* @author maofeng
-
*
-
*/
-
class Mf_Helpers_File extends Zend_Controller_Action_Helper_Abstract
-
{
-
-
public function __construct(){
-
-
}
-
-
/**
-
* file_get_contents()
-
*
-
* Enter description here ...
-
* @param unknown_type $fileInfo 包含文的路径信息,例如 /home/wwwroot/
-
* $fileInfo = array('public', 'attachment', 'demo.txt');
-
*/
-
public function read($fileInfo = array()){
-
$result = '';
-
-
if(!empty($fileInfo)){
-
$str = '';
-
foreach ($fileInfo as $v){
-
$str .= '/'.$v;
-
}
-
$str = trim($str, '/');
-
}
-
-
$realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
-
$attachmentPath = $realPath.$str;
-
-
if(file_exists($attachmentPath)){
-
$result = file_get_contents($attachmentPath);
-
}else{
-
//echo '文件不存在';exit;
-
return false;
-
}
-
-
return $result;
-
}
-
-
/**
-
* file_put_contents()
-
* Enter description here ...
-
* @param unknown_type $fileInfo
-
* @param unknown_type $content
-
* $fileInfo = array('public', 'attachment', 'demo.txt');
-
*/
-
public function write($fileInfo = array(), $content = ''){
-
-
if(!empty($fileInfo)){
-
$str = '';
-
foreach ($fileInfo as $v){
-
$str .= '/'.$v;
-
}
-
$str = trim($str, '/');
-
}
-
-
$realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
-
$attachmentPath = $realPath.$str;
-
-
$arr = explode('/', trim($attachmentPath, '/'));
-
-
$dir = '';
-
for($i=0; $i<(count($arr)-1); $i++){
-
$dir .= '/'.$arr[$i];
-
}
-
-
if(is_dir($dir) && is_writable($dir)){
-
$byte = file_put_contents($attachmentPath, $content);
-
if($byte > 0){
-
return true;
-
}else{
-
return false;
-
}
-
}else{
-
return false;
-
}
-
-
}
-
-
/**
-
*
-
*
-
* Enter description here ...
-
* @param unknown_type $fileName 要上传的文件源信息
-
* @param unknown_type $fileInfo 上传目标文件的信息 array('public', 'attachment') 代表/public/attachment/目录下
-
* @param unknown_type $newFileName 新上传文件的名字,一般会重命名,即重命名 default = 'newName' ,
-
* @param unknown_type $limit 允许上传的文件的后辍限制
-
* $fileInfo = array('public', 'attachment', 'demo.txt');
-
*/
-
public function upload($fileName = '', $pathInfo = array(), $newFileName = 'newName', $limit = array('tailer'=>array('txt'), 'size'=>102400 )){
-
-
if(is_uploaded_file($_FILES[$fileName]['tmp_name'])){
-
-
if(!empty($pathInfo)){
-
$str = '';
-
foreach ($pathInfo as $v){
-
$str .= '/'.$v;
-
}
-
$str = trim($str, '/');
-
}
-
-
$realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
-
$dir = $realPath.$str;
-
-
if(is_dir($dir) && is_writable($dir)){
-
-
$oldName = $_FILES[$fileName]['name'];
-
-
$cacheArray = explode('.', $oldName);
-
-
$tailer = $cacheArray[count($cacheArray) - 1];
-
-
if(in_array($tailer, $limit['tailer'])){
-
-
$newName = $newFileName.'.'.$tailer;
-
-
$destination = $dir.'/'.$newName;
-
-
move_uploaded_file($_FILES[$fileName]['tmp_name'], $destination);
-
-
}
-
-
}
-
-
}
-
}
-
-
-
public function download($fileInfo = array(), $newName = 'newFile.txt'){
-
-
-
$result = '';
-
-
if(!empty($fileInfo)){
-
$str = '';
-
foreach ($fileInfo as $v){
-
$str .= '/'.$v;
-
}
-
$str = trim($str, '/');
-
}
-
-
$realPath = Mf_Configs_Config::getInstance()->getConfig('realPath');
-
$attachmentPath = $realPath.$str;
-
-
if(file_exists($attachmentPath)){
-
-
header("Content-Type:application/octet-stream");
-
header("Content-Disposition: attachment; filename=$newName");
-
-
header("Pragma: no-cache");
-
header("Expires: 0");
-
echo file_get_contents($attachmentPath);
-
exit;
-
-
}
-
-
}
-
-
-
-
-
}
阅读(1208) | 评论(0) | 转发(0) |