1.html
<script language="javascript" src="validator.js"></script>
<script language="javascript">
function upmyimage()
{
var imgvalue = document.getElementById("myimg").value;//file文件值
var mypic = document.getElementById("mypic"); //显示头像
//为了避免转义反斜杠出问题,这里将对其进行转换
var re = /(//+)/g;
var filename = imgvalue.replace(re,"#");
//对路径字符串进行剪切截取
var one = filename.split("#");
//获取数组中最后一个,即文件名
var two = one[one.length-1];
//再对文件名进行截取,以取得后缀名
var three = two.split(".");
//获取截取的最后一个字符串,即为后缀名
var last = three[three.length-1];
//添加需要判断的后缀名类型
var tp = "jpg,gif,JPG,GIF";
//返回符合条件的后缀名在字符串中的位置
var rs = tp.indexOf(last);
if(imgvalue != '')
{
//如果返回的结果大于或等于0,说明包含允许上传的文件类型
if(rs<0)
{
alert("您选择的上传文件不是有效的图片文件!");
return false;
}
else
{
mypic.src = imgvalue;
document.myform1.action = "/upmypic.php";
document.myform1.target = "hidiframe";
document.myform1.submit();
document.myform1.action = "infocomm.php";
document.myform1.target = "_self";
}
}
}
</script>
</head>
<body>
<form name="myform1" method="post" enctype="multipart/form-data" action="infocomm.php" onSubmit="return Validator.Validate(this,3)">
<table width="790" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" align="right">电子邮箱:</td><td><input type="text" name="us_mail" size="20" dataType="Email" msg="信箱格式不正确"><font color=red></font></td>
</tr><tr><td align="right">昵称</td><td><input type="text" name="us_dearname" size="20"</td></tr><tr><td width="100" align="right" valign="top">上传头像:</td>
<td>
<a href='图片链接地址' target=_blank><img class="pic" id=mypic src='' border=0 vspace=0 hspace=0 width=92 height=92></a><span class="red12">*图片支持jpg、gif格式,大小在200K以内,160*160像素</span></td></tr><tr><td width="100" align="right"></td><td><input type="file" name="us_pic" id="myimg" value="" onChange="return upmyimage();" size="16" require="false" dataType="Filter" msg="非法的文件格式" accept="jpg, gif, png"><div id="m"></div></td></tr></tr><tr><td width="100"></td><td height="30" align=center><input type=button value="保 存" onClick="if (Validator.Validate(myform1,3)) myform1.submit();"> <input type="reset" value="重 置"></td>
</tr>
</table>
</form><iframe id="hidiframe" style="display:none" width="0" height="0"></iframe>
2.upmypic.php
<?
require_once "upload.php";
$path = '../pic/mebasic/' . $usid % 999;
if($_FILES['us_pic']['name'] <> '')
{
//实例化类,上传图片
$upimg = new Upload($_FILES['us_pic'], $path, '204800');
$upimg->checkFileAndUpload();
if(count($upimg->_namearray) > 0)//有图片上传成功
{
foreach($upimg->_namearray as $value)
{
$imgname = "http://www.niutian365.com/".$path,3).$value;
}
}
}
if ($imgname <> "")
{
$ssql = "update me_basic_info set us_pic = '$imgname' where usid = '" . $usid . "'";
if(mysql_query($ssql, $db_link))
{
echo "<script>alert('头像已更新');</script>";
}
else
{
echo "<script>alert('未知错误请重新上传!');</script>";
}
}
?>
3.upload.php
<?
class Upload
{
var $_object; //文件对象
var $_upPath; //上传文件的保存路径(必须以"/"结尾)
var $_curname; //当前上传的图片名称
var $_allowExt; //允许上传的文件类型
var $_maxSize; //允许上传的大小
var $_log; //上传日志
var $_namearray; //上传成功的新文件名
//构造函数(上传对象, 上传路径, 允许大小, 新名称, 允许扩展名)
function Upload($_object, $path="", $size="", $name="", $ext="")
{
!is_array($_object) && $this->Error("上传对象未设置", "echo");
$this->_object = $_object;
$this->_upPath = $path == "" ? "upload/" : $path;
$this->_maxSize = $size == "" ? 1024*1024*2 : $size;
$this->_allowExt = $ext == "" ? "jpg/gif" : $ext;
$this->_curname = "";
$this->_log = "";
$this->_namearray = array();
$this->setPath();
}
//获取上传文件的扩展名
function getFileExt($filename)
{
$pos = strrpos($filename,".");
$pos === false && $this->Error("文件格式不正确");
$ext_name = substr($filename,$pos+1);
return strtolower($ext_name);
}
//设置上传路径
function setPath($path="")
{
$path <> '' && $this->_upPath = $path;
!is_dir($this->_upPath) && mkdir($this->_upPath);
}
//检测要上传的文件是否符合要求
function checkFileAndUpload()
{
if(!is_array($this->_object["name"])) //只上传一个文件
{
$this->_curname = $this->_object["name"];
$fileext = $this->getFileExt($this->_curname);
$filesize = $this->_object["size"];
if(strpos($this->_allowExt,$fileext) === false)
{
$this->setLog("$this->_curname是不合法的文件类型/n");
}
elseif($filesize > $this->_maxSize)
{
$this->setLog("$this->_curname的大小超过限制/n");
}
else
{
$this->uploadFile($this->_object['tmp_name'], $fileext);
}
}
else //批量上传
{
for($i = 0; $i < count($this->_object["name"]); $i++)
{
if($this->_object["name"][$i] == '')
{
continue;
}
$this->_curname = $this->_object["name"][$i];
$fileext = $this->getFileExt($this->_curname);
$filesize = $this->_object["size"][$i];
if(strpos($this->_allowExt,$fileext) === false)
{
$this->setLog("$this->_curname是不合法的文件类型/n");
}
elseif($filesize > $this->_maxSize)
{
$this->setLog("$this->_curname的大小超过限制/n");
}
else
{
$this->uploadFile($this->_object['tmp_name'][$i], $fileext);
}
}
}
}
//上传函数
function uploadFile($tmpname, $fileext)
{
$ran = rand(); //产生随机数
$newname = date("YmdHis").$ran.".".$fileext;
file_exists($newname) && $this->setLog($this->_curname."与其他文件名冲突");
if(@move_uploaded_file($tmpname, $this->_upPath.$newname))
{
$this->setLog($this->_curname."上传成功//n");
$this->_namearray[] = $newname;
}
}
//记录上传日志
function setLog($log)
{
$this->_log.= $log;
}
//错误提示
function Error($msg,$type='alert')
{
switch($type)
{
case 'alert':
echo "<script>alert('$msg');history.go(-1)</script>";
break;
case 'echo':
echo $msg;
break;
}
exit;
}
}
?>
4.infocomm.php
<?
..........................
$ssql = "update info set
uname = '" . $uname . "'
, usex = '" . $usex . "'
, uhow = '" . $uhow . "'
where id = '" . $id . "'";
mysql_query('set names gbk', $link);
mysql_query($ssql, $link);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<script language="javascript">
alert("新资料已保存!");
location.href = "~~~~~~";
</script>
</head>
</html>