<?php
header("content-type:text/html;charset=utf-8");
$arr_ext = array("jpg","png","gif");
$index = strrpos($_FILES['upload']['name'],".");
$ext = substr($_FILES['upload']['name'],$index+1);
if(!in_array($ext,$arr_ext))
{
 echo "上传的文件不是图像";
 exit();
}else
{
 $src = $_FILES['upload']['tmp_name'];
 $dst = "./upload/".uniqid().".$ext";
 move_uploaded_file($src,$dst);
 echo "上传文件成功";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>判断上传文件是否为图片,为保存为唯一命名</title>
</head>
<body>
 <form name="form1" method="post" enctype="multipart/form-data" action="upload2.php">
 上传图片:<input type="file" name="upload">
 <input type="submit" value="提交表单">
 </form>
</body>
</html>

 

发表评论

Close Menu