温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

thinkphp之文件上传的示例

发布时间:2021-01-29 15:35:52 来源:亿速云 阅读:241 作者:小新 栏目:编程语言

小编给大家分享一下thinkphp之文件上传的示例,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

tp5的文件上传,具体代码详情如下:

模板页(一定要加上enctype="multipart/form-data"):

 <form action="{:url('index/index/upQuestionsWrite')}" method="post" class="form form-horizontal" enctype="multipart/form-data" id="addform">             <div class="row cl">           <label class="form-label col-xs-4 col-sm-2">选择试题文件:</label>           <div class="btn-upload form-group">               <input type="text" name="uploadfile" id="uploadfile" class="input-text upload-url radius" readonly><a href="javascript:void();" class="btn btn-primary radius"><i class="Hui-iconfont"></i>浏览文件</a>               <input type="file" name="examfile" class="input-file" multiple>                          </div>               <boutton type="submit" class="btn btn-success btn-submit">导入试题</button>           </div>           </form>

控制器(在tp5中获取上传文件的文件名称与tp3略有不同):

public function upQuestionsWrite()       {           // 获取表单上传文件           $file = request()->file('examfile');           if(empty($file)) {               $this->error('请选择上传文件');           }           // 移动到框架应用根目录/public/uploads/ 目录下           $info = $file->move(ROOT_PATH.'public'.DS.'upload');          //如果不清楚文件上传的具体键名,可以直接打印$info来查看           //获取文件(文件名),$info->getFilename()  ***********不同之处,笔记笔记哦         //获取文件(日期/文件名),$info->getSaveName()  **********不同之处,笔记笔记哦         $filename = $info->getSaveName();  //在测试的时候也可以直接打印文件名称来查看          if($filename){                           $this->success('文件上传成功!');           }else{               // 上传失败获取错误信息               $this->error($file->getError());           }       }                  // 上传失败获取错误信息                 $this->error($file->getError());             }         }

看完了这篇文章,相信你对“thinkphp之文件上传的示例”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI