日期:2014-02-21  浏览次数:20443 次

PHP代码:--------------------------------------------------------------------------------

<?
/*
------------------------------------------------------------------------------------
类名:Lwguploadandquery
说明:sql语句执行和文件上传类,是Lwgupload类的子类
作者:龙卫国
网络user:lwg888
邮箱:lwg888@163.com
使用、修改、传播请保留作者信息
------------------------------------------------------------------------------------
*/

require_once(dirname(__FILE__)."/Lwgdb.inc.php");//数据库连接与查询类
require_once(dirname(__FILE__)."/Lwgupload.inc.php");//父类
class Lwguploadandquery extends Lwgupload{
var $sql;//sql语句
var $url;//文件上传成功后转向的url

//重要提示:sql语句写法:
//$sql=sprintf("update test1 set name='%s', image1='%s', image2='%s' where id='%d'",$_POST['name'],"image1_name","image2_name",$_POST['id']);
//"image1_name","image2_name"中,image1和image2是文件域的name或id,后面加了"_name"后缀,便于下面找到它并修改它
function Lwguploadandquery($sql="",$uploadfield="",$url="",$uploadpath="",$maxsize="",$ftype="all"){
$this->sql=$sql;
$this->url=$url;
$this->Lwgupload($uploadfield,$uploadpath,$maxsize,$ftype);
}

function run(){
if (empty($this->sql))return $this->output("没有可执行的SQL语句。");

if (!is_array($this->uploadfield))$this->uploadfield=array($this->uploadfield);//如果不为数组改为数组,便于后面代码的简化
if (!is_array($this->maxsize))$this->maxsize=array($this->maxsize);//同上
if (!is_array($this->ftype))$this->ftype=array($this->ftype);//同上

$totaluploadfield=count($this->uploadfield);
for ($i=0;$i<$totaluploadfield;$i++){
$uploadfile=$_FILES[$this->uploadfield[$i]]['tmp_name'];
$file_name=$_FILES[$this->uploadfield[$i]]['name'];
$file_old=$_POST[$this->uploadfield[$i].'_old'];//用在更新中,表示上传文件所对应的原有文件
$file_del=$_POST[$this->uploadfield[$i].'_del'];//原有文件是否标记为删除

if ($uploadfile!=""&&$file_name!=""){
//表示需要上传,即文件域里填入了任何字符
$uploadfield[]=$this->uploadfield[$i];
//取得新的上传字段,因为文件域中没有填入任何字符的不用上传
$maxsize[]=(!empty($this->maxsize[$i]))?$this->maxsize[$i]:$this->maxsize[0];
//取得相对应的大小限制值
$ftype[]=(!empty($this->ftype[$i]))?$this->ftype[$i]:$this->ftype[0];
//取得相对应的类型限制值
$newfile=$this->uploadpath."/".$file_name;
//上传后的文件地址
$insertname=$file_name;
//修改sql语言时要用到,将mysql表中对应字段的值改为目前上传文件名
if ($file_old!="")$oldfile[]=$this->uploadpath."/".sprintf('%s',$file_old);
//用在更新中,取得原有文件地址,当新文件上传后删除旧文件
}
else if ($file_old!=""){
//表示不需要上传,即文件域里没有填入任何字符,但是有旧文件
if ($file_del=="true"){
//如果旧文件标记为删除
$insertname="";
//修改sql语言时要用到,将mysql表中对应字段的值改为空
$oldfile[]=$this->uploadpath."/".sprintf('%s',$file_old);
}
else $insertname=sprintf('%s',$file_old);
//既不用上传也不用删除,修改sql语言时要用到,将mysql表中对应字段的值改为原文件名
}
else $insertname="";
//既不用上传也没有原文件,修改sql语言时要用到,将mysql表中对应字段的值改为空
$this->sql=str_replace($this->uploadfield[$i]."_name", $insertname, $this->sql);
//修改sql语句
}

$this->uploadfield=$uploadfield;
$this->maxsize=$maxsize;
$this->ftype=$ftype;

if (count($uploadfield)>0){
if (!$this->test($oldfile))return false;
}
//测试能否全部上传