<!Doctype html>
<html>
<head>
	<meta charset="UTF-8">
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
    <input type="hidden" name="action" value="upload" />
    <input type="file" name="u_file"/>
    <input type="submit" value="上传" />
</form>
</body>
</html>
<?php
        if(isset($_POST['action']) && $_POST['action'] == "upload"){            //判断提交按钮是否为空
        $file_path = "./uploads\\";         //定义图片在服务器中的存储位置
        $picture_name=$_FILES['u_file']['name'];    //获取上传图片的名称
        $picture_name=strstr($picture_name , ".");  //通过strstr()函数截取上传图片的后缀
        if($picture_name!= ".jpg"){                 //根据后缀判断上传图片的格式是否符合要求
            echo "<script>alert('上传图片格式不正确,请重新上传'); window.location.href='index.php';</script>";
        }else if($_FILES['u_file']['tmp_name']){
            move_uploaded_file($_FILES['u_file']['tmp_name'],$file_path.$_FILES['u_file']['name']);//执行图片上传
            echo "图片上传成功!";
        }
        else
            echo "上传图片失败";
    }
?>
