一、建立html网页页面
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="ufile">
<input type="submit" value="提交">
</form>
提交文件操作当然是要用到表单标签form啦,但这里一定要用POST,且要加上 enctype="multipart/form-data" 这一段代码
二、操作
<?php
$src='img/';
$ufile=$_FILES['ufile'];
if(copy($ufile['tmp_name'],"你想要存储的位置".$ufile['name'])){
echo "成功!";
echo "<img src=".$src.$ufile['name'].">";
}else echo "失败!";
?>
具体的$_FILES变量上传的操作看这里 - -> PHP文件上传 (biancheng.net)
效果图:
完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="ufile">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
$src='img/';
$ufile=$_FILES['ufile'];
if(copy($ufile['tmp_name'],"上传文件保存的位置".$ufile['name'])){
echo "成功!";
echo "<img src=".$src.$ufile['name'].">";
}else echo "失败!";
?>