ajax图片上传的php代码
if (
$_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/jpeg'
&& $_FILES['file']['size'] <= 2 * 1024 * 1024
) {
// 获取文件类型
$arr = explode(".", $_FILES['file']['name']);
$imgtype = end($arr);
// 获取文件路径
$imgurl = 'images/' . time() . rand(1000, 9999) . '.' . $imgtype;
// 文件转移
move_uploaded_file($_FILES['file']['tmp_name'], $imgurl);
}
$i = $_COOKIE['img'];
$res = [];
setcookie('img', $i);
$res['code'] = 1;
$res['msg'] = $imgurl;
echo json_encode($res);