--------------------------html------------------------------------------------------html-------------------------------
<!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=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>图片上传</title>
</head>
<body>
<form action="shc.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="max_file_size" value="33554432">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="上传" οnclick="" />
</form>
<script type="text/javascript">
function subup(){
var file=document.getElementById("file").value;
if(file==''){alert("选择文件...");return false;}
}
</script>
</body>
</html>
--------------------------php------------------------------------------------------php--------------------------------
<?php
//上传文件类型列表
$uptypes=array(
'image/jpg','image/jpeg','image/png','image/pjpeg',
'image/gif','image/bmp','image/x-png'
);
$width_size=500; //规定图片最大宽度
$height_size=500; //规定图片最大高度
$max_file_size=417200; //上传文件大小限制, 单位BYTE
$uploads="uploads/"; //上传文件路径
$imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2; //缩略图比例
//检查是否是Post提交
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//是否存在文件
if (!is_uploaded_file($_FILES["file"]['tmp_name']))exit("文件不存在");
$file = $_FILES["file"];
$filename=$file["tmp_name"]; //获取临时文件名
$image_size = getimagesize($filename); //获取图片相关信息
//判断图片像素是否超过设定值
if($image_size[0]>$width_size||$image_size[1]>$height_size)exit("图片超过规定像素");
//检查文件大小
if($max_file_size < $file["size"])exit("文件太大");
//检查文件类型
if(!in_array($file["type"], $uptypes))exit("文件类型不符");
//检查存储路劲
if(!file_exists($uploads))mkdir($uploads);
$pinfo=pathinfo($file["name"]); //获取文件名相关信息
$ftype=$pinfo['extension']; //获取文件扩展名(后缀)
$destination = time().".".$ftype; //以时间戳命名新文件
//检查是否有同名文件
if (file_exists($destination) && $overwrite != true)exit("同名文件已存在");
//返回上传状态
if(!move_uploaded_file ($filename,$uploads.$destination))exit("移动出错");
//生成缩略图
if($imgpreview==1){
echo "<img src=\"".$uploads.$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize).">";
}
}
?>