赞
踩
public function uploads() { // 获取表单上传文件 例如上传了001.jpg $file = request()->file('thumbnail'); // 移动到框架应用根目录/public/uploads/ 目录下 if ($file) { $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { // 成功上传后 获取上传信息 // 输出 jpg $thumb = '/uploads' . '/' . $info->getSaveName(); $thumb = str_replace('\\', '/', $thumb); return json_encode($thumb); } else { // 上传失败获取错误信息 echo $file->getError(); } } }
原理是
首先request()->file(‘thumbnail’) 获取表单中名为 “thumbnail” 的文件上传字段的文件对象。
获取到了之后
将文件移动到框架应用根目录下的 “public/uploads/” 目录中。
最后会返回路径给前端。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。