首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP与模板与代码加密/优化 » [原创]新手请教 帮帮忙。谢谢啦  


  作者:nakeself
  注册时间:2005-11-25
  主题/回复:5/1
  积分:426
  等级:★★☆(五级)
  称号:略有小成

  nakeself@yahoo.c..
  27759634
  www.

 

 发表:2006-03-31 22:46:15 阅读 2350 次 回复 2 次 得分1  |   字号 字色
[原创]新手请教 帮帮忙。谢谢啦
我有一个类 但是我不会用啊。 大家帮帮我哈。帮我写一个怎么使用的例子  。谢谢啦。。。
<?
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;
}
}
?>
 
 1#楼  
 
  回复人:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

   
 1#楼 发表于2006-04-01 01:51:10  评分:1 

这是一个处理文件上传的类
类里面定义了一系列的方法来完成各种操作。

类的使用最好直接咨询类的作者,或者类的技术支持网站来获得使用方法。
其他人要分析这个类,倒是也可以分析出来大体的思路,但是写一个程序来使用还是比较麻烦,很多参数类型不好确定,还有很多方法的使用前后顺序判断起来难度很大。因此直接写出一个例子较为困难,需要多次实验、摸索。

就比如PHP如果不提供使用手册,很多函数我们不知道如何去使用。
 2#楼  
 
  回复人:nakeself
  注册时间:2005-11-25
  主题/回复:5/1
  积分:426
  等级:★★☆(五级)
  称号:略有小成

   
 2#楼 发表于2006-04-01 07:44:49  评分:× 

恩,谢谢。因为我基础不太好,不知道PHP的函数标标准
我只是想要一个最简单的代码,因为别人使用这个类是可以上传,但是我使用却不行。就是因为我不知道具体的该怎么去使用,什么样的代码才是标准的,所以不知道哪儿错了。很郁闷啊。
<?php
echo $_FILES["f"]["name"];
if ( $_POST["sub"] == 1) {
include_once( "Uploader.php" );
$u = new Uploader( "upload", "uploader");
$u->addFile( $_FILES["f"]);
$u->upload();
}
?>

<form action="" method="post" enctype="multipart/form-data"  >
<input type="file" name="f">
<input type="submit" name="sub" value="1">
</form>
这个代码可以显示出文件名,但是上传不上去,我不知道哪儿没对 是不是路径呢 我改了很多种路径写法也不对。
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.011sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版