湃居骚炽倪邢腐描搅缆耸渡龄
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<h3>文件上传</h3> <form action="{:url('upload')}" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <p><button>上传</button></p> </form>
在index控制器下创建upload()
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } //处理上传的文件 public function upload() { return "上传成功!"; } }
执行:
修改upload(),处理文件上传
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } //处理上传的文件 public function upload() { // 1.获取文件信息 // 参数必须与 demo3.html 中<input type="file"name="file">的 name 值一致 $file=$this->request->file('file');//返回值为对象 dump($file); } }
执行:
修改控制器,完善上传
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } //处理上传的文件 public function upload() { // 1.获取文件信息 // 参数必须与 demo3.html 中<input type="file"name="file">的 name 值一致 $file=$this->request->file('file');//返回值为对象 // 2.移动文件到服务器上的指定目录:public/uploads //如果上传成功,返回的是一个已经赋值的文件对象,失败返回 false $res=$file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads'); // 3.对上传文件进行验证:文件大小,文件类型 if($res==false) { return $this->error($file->getError()); } $this->success('上传成功!'); } }
创建uploads目录
执行:
上传符合规则的文件
bug:此时不选择文件就点击上传会报错
修改控制器:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } //处理上传的文件 public function upload() { // 1.获取文件信息 // 参数必须与 demo3.html 中<input type="file"name="file">的 name 值一致 $file=$this->request->file('file');//返回值为对象 //判断用户是否选择文件 if(is_null($file)) { $this->error("没有选择文件"); } // 2.移动文件到服务器上的指定目录:public/uploads //如果上传成功,返回的是一个已经赋值的文件对象,失败返回 false $res=$file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads'); // 3.对上传文件进行验证:文件大小,文件类型 if($res==false) { return $this->error($file->getError()); } $this->success('上传成功!'); } }
执行:
获取上传文件的存储路径和保存文件名:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { public function index() { return $this->fetch(); } //处理上传的文件 public function upload() { // 1.获取文件信息 // 参数必须与 demo3.html 中<input type="file"name="file">的 name 值一致 $file=$this->request->file('file');//返回值为对象 //判断用户是否选择文件 if(is_null($file)) { $this->error("没有选择文件"); } // 2.移动文件到服务器上的指定目录:public/uploads //如果上传成功,返回的是一个已经赋值的文件对象,失败返回 false $res=$file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads'); // 3.对上传文件进行验证:文件大小,文件类型 if($res==false) { return $this->error($file->getError()); } // 获取上传文件的保存路径 echo '上传文件的存储路径为:' .$res->getSaveName().'<br>'; // 获取上传文件的保存文件名 echo '上传文件的存储名称为:' .$res->getFilename().'<br>'; } }
执行:
崇窟醒冀放哇夺垮客巳虫趁瓢