html代码
<!DOCTYPE html>
<html lang="en">
<head></head>
<body class="templatemo-bg-gray">
<div class="container">
<div class="col-md-12">
<form class="form-horizontal templatemo-container margin-bottom-30" action="action.php" method="post" enctype="multipart/form-data">
<div class="form-group 1">
<div class="col-xs-12" style="padding:0;">
<div class="control-wrapper" style="padding:0;">
<table class="table table-striped table-bordered table-hover">
<tr>
<th class="center">图片</th>
</tr>
<tr >
<td class="center">
<input type="file" class="form-control" name="file" placeholder="上传">
</td>
</tr>
</table>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
其中form表单中enctype="multipart/form-data"必须要有,其中w3c中是这样描述
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。
默认地,表单数据会编码为 "application/x-www-form-urlencoded"。就是说,在发送到服务器之前,所有字符都会进行编码(空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值)。
上传文件时必须使用该值
php后台
$file = $_FILES["file"];
if(!empty($_FILES["file"]["tmp_name"])){
var_dump($FILES["file"]["tmp_name"]["size"]);
$name = $file['name'];
$type = strtolower(substr($name,strrpos($name,'.')+1));
$allow_type = array('jpg','jpeg','gif','png');
if(!in_array($type, $allow_type)){
return ;
}
if(!is_uploaded_file($file['tmp_name'])){
return ;
}
$upload_path = "img/";
$name1 = $file['name'];
$namearr = explode(".",$name1);
$filename = $namearr[1];
if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
rename($upload_path.$file['name'],$upload_path.$filename);
}else{ }
}else{
$imgupload = false;
}
OK,妥妥的搞定。