总结:这个文件上传需要的数据是headers: { ‘Content-Type‘: ‘multipart/form-data‘ }
意思就是 formdata格式 要不然后端request是接收不到数据的
下面是实际操作:
前端uniapp 用了uview的上传组件,<u-form :model="form" ref="uForm"></u-form>导致php端无法通过 $file = $this->request->file(‘file‘); 这个来获取到上传的文件
所以php后端 需要用 $_FILES[‘img_mentou‘][‘tmp_name‘] 来获取
demo:
$imageName =time()."_".rand(1111,9999).‘.jpg‘;
$path = "uploads/ziliao/".date("Ymd",time());
if (!is_dir($path)){ //判断目录是否存在 不存在就创建
mkdir($path,0777,true);
}
$imageSrc= $path."/". $imageName; //图片名字
// $r= file_put_contents(ROOT_PATH ."public/".$imageSrc, $file);
if(file_exists($_FILES[‘img_mentou‘][‘tmp_name‘])){
// $r= $file->move(ROOT_PATH ."public/".$imageSrc);
$r= move_uploaded_file($_FILES["img_mentou"]["tmp_name"],ROOT_PATH ."public/".$imageSrc);
if($r){
$this->success(‘返回成功‘);
}else{
$this->error(‘上传失败‘);
}
}
原文:https://www.cnblogs.com/weilianguang/p/14066044.html