//包含表单文件上传的处理,类型/大小的限定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Guitar Wars - Add Hight Score</title>
<link rel="stylesheet" type="text/css" href="score.css">
</head>
<body>
<h2>Guitar Wars - Add Hight Score.</h2>
<?php
// define('UPLOADPATH', 'images/');
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES['screenshot']['name'];//获取文件名
$screenshot_type = $_FILES['screenshot']['type'];// 类型格式 image/png
$screenshot_size = $_FILES['screenshot']['size'];
if (!empty($name) && !empty($score) && !empty($screenshot)) {
include 'dbc.php';
//检测文件类型及大小限制
if (($screenshot_type == 'image/png' || $screenshot_type == 'image/gif' || $screenshot_type == 'image/jpeg') && ($screenshot_size > 0) && ($screenshot_size <= MAXFILESIZE)) {
//确定好图片路径
$target = UPLOADPATH.time().$screenshot;
if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
//增加了时间变量 需要与路径名一直
$screenshot = time().$screenshot;
$query = "insert into guitar values (0, NOW(),'$name','$score','$screenshot')";
mysqli_query($dbc,$query);
echo '<p>Thanks for adding your new hight score</p>';
echo '<p><strong>Name:</strong>' .$name.'</p>';
echo '<p><strong>Score:</strong>' .$score.'</p>';
echo '<img src="'.$target.'" alt="score image">';
echo '<p><a href="guitar_index.php"><< Back to hight scores</a></p>';
$name ='';
$score ='';
}
}else{
echo '<p class="error">The file must be GIF/JPEG/PNG and max file size less '.(MAXFILESIZE/1024).'KB. </p>';
}
@unlink($_FILES['$screenshot']['tmp_name']);//删除临时文件
}else{
echo '<p class="error">Please enter all of the information to add your hight score.</p>';
}
}
?>
<hr />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<!-- 最大上传32k -->
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAXFILESIZE; ?>" />
<label for="score"> Score:</label>
<input type="text" name="score" id="score" value="<?php if (!empty($score)) echo $score; ?>"><br>
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php if (!empty($name)) echo $name; ?>"><br>
<label for="screenshot">Screen shot:</label>
<input type="file" name="screenshot" id="screenshot">
<hr />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>