Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* 文件管理类
|
||||
* @author Nintendov
|
||||
*/
|
||||
|
||||
namespace Vin;
|
||||
|
||||
class FileStorage{
|
||||
|
||||
/**
|
||||
* 操作句柄
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
static protected $handler ;
|
||||
|
||||
/**
|
||||
* 连接分布式文件系统
|
||||
* @access public
|
||||
* @param string $type 文件类型
|
||||
* @param array $options 配置数组
|
||||
* @return void
|
||||
*/
|
||||
static public function connect($type='File',$options=array()) {
|
||||
$class = 'Vin\\FileStorage\\Driver\\'.ucwords($type);
|
||||
self::$handler = new $class($options);
|
||||
}
|
||||
|
||||
static public function __callstatic($method,$args){
|
||||
//调用缓存驱动的方法
|
||||
if(method_exists(self::$handler, $method)){
|
||||
return call_user_func_array(array(self::$handler,$method), $args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* 本地文件处理类
|
||||
* @author Nintendov
|
||||
*/
|
||||
|
||||
namespace Vin\FileStorage\Driver;
|
||||
use Vin\FileStorage;
|
||||
|
||||
class File extends FileStorage{
|
||||
/**
|
||||
* 本地写文件
|
||||
*/
|
||||
public function put($rootpath,$filename, $content,$maxSize=-1, $cover = TRUE){
|
||||
$filename = '.'.$rootpath.$filename;
|
||||
if($maxSize!=-1){
|
||||
if(strlen($content>$maxSize)){
|
||||
return '文件大小超过限制';
|
||||
}
|
||||
}
|
||||
$dir = dirname($filename);
|
||||
if(!is_dir($dir))
|
||||
mkdir($dir,0755,true);
|
||||
if(false === file_put_contents($filename,$content)){
|
||||
E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
|
||||
}else{
|
||||
$this->contents[$filename]=$content;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历获取目录下的指定类型的文件
|
||||
* @param $path
|
||||
* @param array $files
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function listFile($rootpath, $path ,$allowFiles='all'){
|
||||
$path = $_SERVER['DOCUMENT_ROOT'].__ROOT__.$rootpath.$path;
|
||||
return $this->getList($path, $allowFiles);
|
||||
}
|
||||
|
||||
public function getList($path ,$allowFiles='all' , &$files = array()){
|
||||
if (!is_dir($path)) return null;
|
||||
if(substr($path, strlen($path) - 1) != '/') $path .= '/';
|
||||
$handle = opendir($path);
|
||||
while (false !== ($file = readdir($handle))) {
|
||||
if ($file != '.' && $file != '..') {
|
||||
$path2 = $path . $file;
|
||||
if (is_dir($path2)) {
|
||||
$this->getList($path2, $allowFiles, $files);
|
||||
} else {
|
||||
if($allowFiles!='all'){
|
||||
if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
|
||||
$files[] = array(
|
||||
'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
|
||||
'mtime'=> filemtime($path2)
|
||||
);
|
||||
}
|
||||
}else{
|
||||
$files[] = array(
|
||||
'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
|
||||
'mtime'=> filemtime($path2)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到路径
|
||||
*/
|
||||
public function getPath($rootpath,$path){
|
||||
$path = __ROOT__.$rootpath.$path;
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @author Nintendov
|
||||
*/
|
||||
|
||||
namespace Vin\FileStorage\Driver;
|
||||
use Vin\FileStorage;
|
||||
|
||||
class Sae extends FileStorage{
|
||||
private $st;
|
||||
|
||||
private $error;
|
||||
|
||||
public function __construct(){
|
||||
if(!function_exists('memcache_init')){
|
||||
header('Content-Type:text/html;charset=utf-8');
|
||||
exit('请在SAE平台上运行代码。');
|
||||
}
|
||||
|
||||
$this->st = new \SaeStorage();
|
||||
|
||||
}
|
||||
|
||||
public function getFilename($filename){
|
||||
if(strpos($filename, __ROOT__)) $filename = str_replace(__ROOT__, '', $filename,1);
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写文件
|
||||
* @param 文件名
|
||||
* @param 文件内容
|
||||
* @param 限制尺寸 默认不限制
|
||||
* @param 是否覆盖 默认是
|
||||
*/
|
||||
public function put($rootpath,$filename, $content,$maxSize=-1, $cover = TRUE){
|
||||
$rootpath = trim($rootpath,'/');
|
||||
|
||||
$filename = $this->getFilename($filename);
|
||||
|
||||
if($maxSize!=-1) if(strlen($content)>$maxSize) return '文件大小超过限制';
|
||||
|
||||
if($cover){
|
||||
if($this->st->fileExists($rootpath,$filename)) $this->st->delete($rootpath,$filename);
|
||||
}
|
||||
|
||||
return $this->st->write($rootpath,$filename,$content);
|
||||
}
|
||||
|
||||
|
||||
public function listFile($rootpath,$path , $allowFiles='all'){
|
||||
$rootpath = trim($rootpath,'/');
|
||||
$path = trim($path,'/');
|
||||
return $this->getList($rootpath, $path,$allowFiles);
|
||||
}
|
||||
/**
|
||||
* 遍历获取目录下的指定类型的文件
|
||||
* @param $path
|
||||
* @param array $files
|
||||
* @return array
|
||||
*/
|
||||
public function getList($domain, $path, $allowFiles='all' , &$list=array()){
|
||||
$allowFiles = 'all';
|
||||
$handle = $this->st->getListByPath($domain , $path , 1000);
|
||||
|
||||
if($handle['dirNum'] > 0){
|
||||
foreach ($handle['dirs'] as $dir) {
|
||||
$dirname = trim($dir['fullName'],'/');
|
||||
$this->getList($domain, $dirname,$allowFiles, $list);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($handle['files'] as $file){
|
||||
if($allowFiles!='all'){
|
||||
if (preg_match("/\.(".$allowFiles.")$/i", $file['fullName'])) {
|
||||
$list[] = array(
|
||||
'url'=> $this->st->getUrl($domain,$file['fullName']),
|
||||
'mtime'=> $file['uploadTime']
|
||||
);
|
||||
}
|
||||
}else{
|
||||
$list[] = array(
|
||||
'url'=> $this->st->getUrl($domain,$file['fullName']),
|
||||
'mtime'=> $file['uploadTime']
|
||||
);
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到路径
|
||||
*/
|
||||
public function getPath($rootpath,$path){
|
||||
$rootpath = trim($rootpath,'/');
|
||||
$url = $this->st->getUrl($rootpath,$path);
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user