20,359
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传图片</title>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post" name="uploadfile" style="margin:0px;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name="upfile" type="file" size="8" />
<input type="submit" value="上传" />
<br />
<?php
@header('Content-type: text/html;charset=UTF-8');
if (isset($_FILES['upfile']['size'])){
if(is_uploaded_file($_FILES['upfile']['tmp_name'])){
$upfile=$_FILES["upfile"];
//获表单数据
$imgname=$upfile["name"];//上传文件的文件名
$imgtype=$upfile["type"];//上传文件的类型
$imgsize=$upfile["size"];//上传文件的大小
$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径
//验证(格式、大小)
if ($imgtype=='image/pjpeg' || $imgtype=='image/jpeg' || $imgtype=='image/jpg' || $imgtype=='image/gif' || $imgtype=='image/png' ){
if ($imgsize<=2000000){
//-------------------------------------
$info = pathinfo($imgname);//取到文件后缀
$newname=date('YmdHis').rand(1000,9999).'.'.$info['extension'];//日期+随机数+后缀构成新名$newname
move_uploaded_file($tmp_name,'comimage/'.$newname); //执行上传操作
if(file_exists('comimage/'.$newname)){
unlink('comimage/'.$newname);//删除原图
}
//-------------------------------------
}else{
echo '上传失败,文件太大!不能超过2M';
echo '<script>window.location="addimg2.php"</script>';
}
}else{
echo '上传失败,格式错误。只支持JPG\GIF\PNG格式的图片!';
}
//删除功能
echo '<a href="imgdel.php?imgname='.$newname.'">删除</a>';
}
}
?> </td>
</tr>
</table>
</form>
</body>
</html>