Blogger Information
Blog 11
fans 0
comment 0
visits 6754
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件的上传操作—8月28日作业2
v1per911的博客
Original
588 people have browsed it

以下代码主要实现jpg、jpeg、png文件的上传功能 

在写作业的时候出现的一个问题是,在form标签中的name属性写的是uploads,而在php代码$_FILES数组的值写的是upload,导致始终提示没有选中文件。经修改后可正常实现功能。

实例

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype='multipart/form-data'>
		<fieldset>
			<legend>File Uploads</legend>
			<input type="file" name="uploads">
		</fieldset>
		<button type="submit" name="submit">upload!</button>
	</form>
</body>
</html>


<?php



//判断提交类型是否正确
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	//判断有没有选择文件
	if (isset($_FILES['uploads'])){
		$fileType = ['image/jpg','image/png','image/jpeg'];
		if (in_array($_FILES['uploads']['type'], $fileType)) {
			if (move_uploaded_file($_FILES['uploads']['tmp_name'], __DIR__.'/upload/'.$_FILES['uploads']['name'])){
		echo '<script>alart(successfull!)</script>';
			}
		}else{
			die('文件类型不支持');
		}
	}else{
		die('没有选中文件');
	}
}else{
	die('提交类型错误!');
}


?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments