总结:这个文件上传需要的数据是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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。