文件上传——终结者Ⅰ

上传|终结者

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;
}
//测试能否全部上传

if (count($oldfile)>0)$this->err_del((count($oldfile)-1),$oldfile);
//通过测试后删除标记为删除的原文件

$db=new Lwgdb();//数据库连接与查询类
if (count($uploadfield)==0){
if(!$db->query($this->sql)){
return $this->output("错误:NO1,提交失败,请重试。");
//NO1表示没有上传文件,且sql语句执行失败
}
else $this->debugstr.="sql语句:'".$this->sql."'执行成功 <br>";
}
else {
if ($this->upload()){
if(!$db->query($this->sql)){
$this->err_del(count($uploadfield)-1,$this->newfile);
//删除上传了的文件
return $this->output("错误:NO2,提交失败,请重试。");
//NO1表示上传文件后,sql语句执行失败
}
else $this->debugstr.="sql语句:'".$this->sql."'执行成功 <br>";
}
}
if (!empty($this->url))header("location:".$this->url);
}
}

/*
--------使用方法------------------------------------------------
$uploadname=array("image1","image2");
$sql=sprintf("update test1 set name='%s', image1='%s', image2='%s' where id='%d'",$_POST['name'],$uploadname[0].'_name',$uploadname[1].'_name',$id);
$obj=new Lwguploadandquery($sql,$uploadname,'','Upload',array(40960,2048),array('jpg,gif','all'));
$obj->run();
或:
$obj=new Lwguploadandquery($sql,$uploadname,'','Upload',array(40960,2048),array('jpg,gif','all'));
$obj->uploadname=array("image1","image2");
$obj->sql=sprintf("update test1 set name='%s', image1='%s', image2='%s' where id='%d'",$_POST['name'],$uploadname[0].'_name',$uploadname[1].'_name',$id);
$obj->url="http://xxxxxx.com";
$obj->uploadpath="upload/image";
$obj->maxsize=1024*5;
$obj->ftype='swf';
$obj->run();
----------------------------------------------------------------
*/
?>

--------------------------------------------------------------------------------

<?
/*
------------------------------------------------------------------------------------
类名:Lwgupload
说明:多文件上传类
作者:龙卫国
网络user:lwg888
邮箱:lwg888@163.com
使用、修改、传播请保留作者信息
------------------------------------------------------------------------------------
*/

class Lwgupload{
//-------------可以设置值的变量------------------------
var $uploadfield;//上传文件的字段名
var $maxsize;//限制上传文件的大小
var $file_old;//需要删除的旧文件
var $uploadpath;//文件的上传路径
var $ftype;//限制上传文件的类型
var $debug=true;//是否显示调试或错误信息

//------------用来获取值的变量---------------------------
var $uploadfile;//上传后的临时文件
var $file_name;//文件名
var $file_size;//文件的大小
var $file_size_format;//格式化后的$file_size
var $file_type;//文件类型
var $debugstr="";//记录调试信息
var $err="";//记录错误信息

//构造函数
//$uploadfield 上传文件的字段名,上传多个文件时可以是数组
//$uploadpath 文件的上传路径,不能为数组
//$maxsize 限制上传文件的大小,上传多个文件时可以是数组,为不同的文件限制不同大小
//$ftype 限制上传文件的类型,上传多个文件时可以是数组,为不同的文件限制不同类型
//$ftype 为‘all’时为不限制类型
//使用时可以将$ftype的值设为array('jpg,gif,png','swf','all')表示为三个上传文件限制类型。第一个文件必须是jpg或gif或png,第二个必须是swf,第三个为任意类型
function Lwgupload($uploadfield="",$uploadpath="",$maxsize="",$ftype="all"){
$this->uploadfield=$uploadfield;
$this->uploadpath=$uploadpath;
$this->maxsize=$maxsize;
$this->ftype=$ftype;
}

//test()用来测试能否全部上传,否则一个也不要上传
//$oldfile表示要删除的文件,用在更新中,删除旧的上传新的
function test($oldfile=''){
if ($this->uploadfield=="")return;
if (empty($this->uploadpath))$this->uploadpath=dirname($_SERVER['PATH_TRANSLATED'])."/Upload";
if (empty($this->maxsize))$this->maxsize=1048576;
if (!is_array($this->uploadfield))$this->uploadfield=array($this->uploadfield);
//如果不为数组改为数组,便于后面代码的简化
$total_upload=count($this->uploadfield);//获得总字段数

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

//如果没有相应路径则建立之
if (!file_exists($this->uploadpath)){
if (!mkdir($this->uploadpath,0700))return output("错误!请手动建立有效路径。");
}

include_once(dirname(__FILE__)."/Lwgfiletype.inc.php");
//该文件包含了数组变量$filetype,记录大多数文件类型对应的扩展名

Set_time_limit(60);//设置超时为60秒

$needupload=array();//经测试可以上传的文件

for ($i=0;$i<$total_upload;$i++){
$this->uploadfile[$i]=$_FILES[$this->uploadfield[$i]]['tmp_name']; //临时文件
$this->file_name[$i]=$_FILES[$this->uploadfield[$i]]['name']; //文件名
$this->file_type[$i]=$_FILES[$this->uploadfield[$i]]['type']; //类型
$this->file_lname[$i]=strtolower(substr( strrchr($this->file_name[$i], "." ), 1 )); //扩展名
$this->file_size[$i]=$_FILES[$this->uploadfield[$i]]['size']; //大小
$this->file_old[$i]=$_POST[$this->uploadfield[$i].'_old']; //需要删除的文件
$this->newfile[$i]=$this->uploadpath."/".$this->file_name[$i];//上传后的地址

$maxsize=(!empty($this->maxsize[$i]) && $this->maxsize[$i]>0)?$this->maxsize[$i]:$this->maxsize[0];

$maxsize_value=$this->format_maxsize($maxsize);//将$maxsize格式化

$thetype=(!empty($this->ftype[$i]))?$this->ftype[$i]:$this->ftype[0];
$ftype=array();
$lname=array();
if ($thetype!="all"){
$tmp_type=explode(",",$thetype);
for ($n=0;$n<sizeof($tmp_type);$n++){
$tmp_t=trim($tmp_type[$n]);
if (!empty($tmp_t)){
$s_tmp=strtolower($tmp_t);
$ftype[]=$filetype[$s_tmp];
$lname[]=$s_tmp;
}
}
}
//分析限制当前类型的参数

if (($this->file_size[$i]==0)||($this->uploadfile[$i]=="")){
return $this->output($this->uploadfield[$i]."上传了无效文件或0字节文件。");
}

if ($thetype!="all" && !in_array($this->file_type[$i],$ftype) && !in_array($this->file_lname[$i],$lname)){
return $this->output("对不起!上传文件格式只能是'".$thetype."'。");
}

if ($this->file_size[$i] > $maxsize){
return $this->output("对不起!文件'".$this->file_name[$i]."'大于'".$maxsize_Value."',上传失败。\n请减小文件到".$maxsize_Value."之内,然后再试。");
}

if (file_exists($this->newfile[$i])){
if ($oldfile=="" || ($oldfile!="" && !in_array($this->newfile[$i],$oldfile)))return $this->output("对不起!文件'".$this->file_name[$i]."'已经存在,上传失败。请更改文件名,然后再试。");
//$oldfile表示要删除的文件,虽然要上传的文件存在,但它将要被删除,因此还是可以上传
}

if (in_array($this->file_name[$i],$needupload)){
//如果两个以上的上传字段上传相同的文件是不允许的
return $this->output("对不起!文件'".$this->file_name[$i]."'重复,上传失败。请更改文件名,然后再试。");
}
$needupload[]=$this->file_name[$i];
}
Set_time_limit(30);//设置超时为30秒
return true;
}

//测试过关后可以上传
function upload(){
if ($this->uploadfield=="")return false;
$total_upload=count($this->uploadfield);

for ($i=0;$i<$total_upload;$i++){
$this->file_size_format[$i]=$this->format_maxsize($this->file_size[$i]);

if (@move_uploaded_file($this->uploadfile[$i],$this->newfile[$i]))
$this->debugstr.="文件'".$this->file_name[$i]."'(".$this->file_size_format[$i].")上传成功。<br>";
else {
if ($i>0)$this->err_del(($i-1),$this->newfile);
//如果有一个上传失败,则删除所有已上传的文件
return $this->output("对不起!文件'".$this->file_name[$i]."'上传失败。请重试。");
}
}
return true;
}

//格式化文件大小数字
function format_maxsize($value){
if ($value<1024)$maxsize_Value = $value."字节";
elseif ($value<1024*1024)$maxsize_Value = number_format((double)($value/1024),2)."kb";
else $maxsize_Value = number_format((double)($value/(1024*1024)),2)."mb";
return $maxsize_Value;
}

//出错后删除已上传的文件
function err_del($i,$thefile){
while ($i>=0){
if (@unlink($thefile[$i])!=false)$this->debugstr.="文件'".$thefile[$i]."'删除成功 <br>";
$i--;
}
}

//记录和输出错误信息
function output($msg){
if ($msg!="")$this->err=$msg;
if ($this->debug)echo "<script language=\"JavaScript\" type=\"text/JavaScript\">alert(\"".$msg."\");history.go(-1)</script>";
return false;
}
}

/*
----------------使用方法-----------------------------------------------
$obj=Lwgupload("image1","",1024*2,"all");
if ($obj->test())$obj->upload();
-------------------------------------------------------------------------
*/
?>

时间: 2024-08-08 07:23:31

文件上传——终结者Ⅰ的相关文章

JavaBean实现多文件上传的两种方法

上传 摘要:本文介绍了JavaBean实现多个文件上传的两种方法,分别是使用http协议和ftp协议实现.首先讲述了http协议传送多个文件的基本格式和实现上传的详细过程,之后简单介绍了使用ftpclient 类实现了ftp方式的上传,最后对这两种方法进行了比较. 关键字:JavaBean .http .ftp .ftpclient JavaBean是一种基于Java的软件组件.JSP对于在Web 应用中集成JavaBean组件提供了完善的支持.这种支持不仅能缩短开发时间(可以直接利用经测试和可

java--uploadify3.1多文件上传

使用uploadify时,建议下载uploadify3.1文档.边做边看.    这是页面端: <label style="color:#15428B;font-weight:bold;">选择文件:</label> <fieldset style="width:300px;height:33px;border:1px solid #99BBE8;text-align:left;COLOR:#000000;FONT-SIZE:12px;font-

JSP利用组件实现文件上传的全攻略

js|攻略|上传 一.首先下载jspsmartupload组件 http://dboy520.51.net/cgi-bin/newjavajia/downcount.php?id=22 (本站地址) http://www.jspsmart.com 二.将目录jspsmartupload/wib_inf/classes中的内容拷贝到网站所在的实际目录中的WEB-INF中(resin是这个目录,其他的可能是classes,具体请查阅jspsmartupload/help/setup.htm) 三.如

html5文件上传

上文介绍了如何通过ajax异步上传文件,html5对file的新接口,可以使得在页面上,对用户也有更好的体验. 页面上要做的,仅仅是添加一个html标签: [cce lang="html"] <input id="track" name="track" type="file" multiple="multiple" onchange="showInfo();" /> [/cc

Spring 文件上传功能

本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: 1 2 3 4 5 <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId>     <version>1.0.2.RELEASE<

zyUpload+struct2完成文件上传

v前言: 最近在写自己的博客网站,算是强化一下自己对s2sh框架的理解.期间遇到了很多问题,这些问题在写之前都考虑过,感觉也就是那样吧.但正真遇到了,也挺让人难受的.就利用zyUpload这个js插件实现文件的上传, 我来谈一谈. vzyUpload下载: https://github.com/hjzgg/zyUpload ,或者可以在网上,随便就可以下载到,只不过提供的网址中的zyUpload是我改过的. vzyUpload界面效果: vzyUpload使用需要注意的几个地方: 说明:zyUp

django 文件上传 验证-django文件上传的格式验证问题,在线等!

问题描述 django文件上传的格式验证问题,在线等! 用django写入个文件上传的页面,需要上传文件名称和文件,且文件名只能为数字: class uploadForm(forms.Form): name = forms.IntegerField() headImg = forms.FileField() 遇到一个问题: 当上传的文件很大时,需要等文件上传完成后才会去验证文件名的格式是否为数字,如果 文件名不是纯数字上传就失败了,又得花很长时间重新上传,这显然是不合理的, django有什么方

http协议- 使用http put把本地文件上传到服务器的的时候碰到的问题

问题描述 使用http put把本地文件上传到服务器的的时候碰到的问题 各位好,我在用libcurl的接口往localhost服务器上传文件里上传文件的时候出现了下面的错误(用的机器系统是linuxmint17,搭建的服务器Apache + PHP5 + MySQL)*** We read 4257 bytes from file<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML 2.0//EN""> 405 Method

文件上传是存在硬盘上,还是存在数据库中?

问题描述 文件上传是存在硬盘上,还是存在数据库中? 如果有比较多的文件上传,上千个文件,大小2-3MB,是存在硬盘上,还是存在数据库中? 存在硬盘上,路径保存在数据库中,是比较方便,但是存在一台server上出问题怎么办?是不是应该转换成google二进制存到数据库(MySQL)中? 解决方案 直接备份文件系统啊,这是最基本的,存到数据库很费劲的.而且你的文件还特别多.不划算了,. 解决方案二: 附件多媒体室存硬盘,其它的文字数据存数据库 解决方案三: 比较通行的办法是,将文件存在硬盘中,数据库