html :
<form id="imageform" method="post" enctype="multipart/form-data" action="http://img.qkhl.net/test/hl_shop_image.php">
<input id="photoimg" type="file" name="photoimg">
<input name="userName" type="text" value="1111">
<input type="submit" value="提交" />
</form>
php:
public function actionPicture() {
//收集数据
$request = Yii::$app->getRequest();
$path = dirname(DIR)."/web/uploads/";
$extArr = array("jpg", "png", "gif");
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$user = $_POST['userName'];
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if (empty($name)) {
$this->ajaxReturn(true, null, '请选择要上传的图片');
exit;
}
$ext = $this->extend($name);
if (!in_array($ext, $extArr)) {
$this->ajaxReturn(true, null, '图片格式错误');
exit;
}
if ($size > (1000 * 1024)) {
$this->ajaxReturn(true, null, '图片大小不能超过1M!');
exit;
}
$image_name = time() . rand(100, 999) . "." . $ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if (move_uploaded_file($tmp, $path . $image_name)) {
$data = array(
'imgSize' => $size,
'userName' => $user
);
$this->ajaxReturn(true, $data, '上传成功');
} else {
$this->ajaxReturn(true, null, '上传出错');
}
exit;
}
}
//获取文件类型后缀
function extend($file_name) {
$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);
return $extend;
}