[原创]新手请教 帮帮忙。谢谢啦 |
我有一个类 但是我不会用啊。 大家帮帮我哈。帮我写一个怎么使用的例子 。谢谢啦。。。
<?
class Uploader{
//检查路径是否有效
//检查文件符合格式
//检查是否存在同名文件
//删除文件
var $filelist=array();
var $savePath="./";
var $returnPath="./";
var $allowformat=array("gif","jpg","png");
var $man=array();//主文件名
var $ext=array();//扩展名
var $returned=array();//返回的名字
var $isError=false;
var $dateformat="YmdHis";
var $overwrite=true;//是否覆盖同名文件
var $deleteable=true;//设置为可以删除
var $message="";
var $template="{man}{ext}";//命名模式
function Uploader($path,$returnPath){
$this->savePath=realpath($path)."/";
$this->returnPath=$returnPath."/";
}
function setTemplate($tpl){
//设置命名模板
$this->template=$tpl;
}
function setDateFormat($format){
$this->dateformat=$format;
}
function setOverWrite($flag){
//是否覆盖
$this->overwrite=$flag;
}
function getMessage(){
return $this->message;
}
function setDeleteable($flag){
//设置是否为可删除属性
$this->deleteable=$flag;
}
function addFile($fileVar,$key=""){
//处理群体上传
//file var 文件变量
//key 文件变量对应的标识
if(is_array($fileVar['name'])){
for($i=0;$i<count($fileVar['name']);$i++){
$nowPointer=count($this->filelist);
if($key!="")$nowPointer=$key.$nowPointer;
$this->filelist[$nowPointer]['name']=$fileVar['name'][$i];
$this->filelist[$nowPointer]['type']=$fileVar['type'][$i];
$this->filelist[$nowPointer]['tmp_name']=$fileVar['tmp_name'][$i];
$this->filelist[$nowPointer]['error']=$fileVar['error'][$i];
$this->filelist[$nowPointer]['size']=$fileVar['size'][$i];
}
}else{
$nowPointer=count($filelist);
if($key==""){
$this->filelist[$nowPointer]=$fileVar;
}else{
$this->filelist[$key]=$fileVar;
}
}
}
function getFile($key=''){
if($key=='')return $this->returned;
else return $this->returned[$key];
}
function upload(){
if(count($this->filelist)==0){
$this->message.="No file to Upload!";
return false;
}
if(!is_dir($this->savePath)){$this->message.="Invalid file save path!";return false;};
if(!$this->checkFormat())return false;//文件格式不和要求
//检查通过
while(list($k,$v)=each($this->filelist)){
//上传
$name=$this->getName($k);
if($this->overwrite){
//覆盖旧文件
if(move_uploaded_file($v['tmp_name'],$this->savePath.$name)){
$this->returned[$k]=$this->returnPath.$name;
if($this->deleteable)
@chmod($this->savePath.$name,0777);
}else {
$this->message.=$v['error'];
$this->isError=true;
}
}else{
if(!is_file($this->savePath.$name)){
//不存在同名文件
if(move_uploaded_file($v['tmp_name'],$this->savePath.$name)){
$this->returned[$k]=$this->returnPath.$name;
if($this->deleteable)
@chmod($this->savePath.$name,0777);
}else {
$this->message.=$v['error'];
$this->isError=true;
}
}else{
//存在同名文件删除
//@unlink($this->savePath.$name);
//if(move_uploaded_file($v['tmp_name'],$this->savePath.$name)){
// $this->returned[$k]=$this->returnPath.$name;
//}else {$this->message.=$v['error'];$this->isError=true;}
$this->message.="File(".$name.") exists! ";
$this->isError=true;
}
}
}
return $this->isError;
}
function getName($filevarkey){
//文件名生成
//{date}按时间 20051212
//{man} 主文件名
//{ext}扩展名
$date=date($this->dateformat);
$man=$this->man[$filevarkey];
$ext=".".$this->ext[$filevarkey];
$name=str_replace("{date}",$date,$this->template);
$name=str_replace("{man}",$man,$name);
$name=str_replace("{ext}",$ext,$name);
return $name;
}
function getExtName(){
if(is_array($this->filelist)){
while(list($k,$v)=each($this->filelist)){
$names=explode('.',$v['name']);
$this->ext[$k]=strtolower($names[count($names)-1]);
unset($names[count($names)-1]);
$this->man[$k]=strtolower(implode('.',$names));
}
}
reset($this->filelist);
}
function checkFormat(){
$this->getExtName();
$flag=true;
while(list($k,$v)=each($this->ext)){
if(!in_array($this->ext[$k],$this->allowformat)){
$this->isError=true;
$this->message.="File ".$this->man[$k].".".$v." Format not support!\r\n";
$flag=false;
}
}
reset($this->ext);
return $flag;
}
}
?> |
|
|