thinkphp5多文件上传如何实现
一、总结
一句话总结:官方文档,测试一下,一定要测试,打印中间变量,就知道每句话是什么意思,一定要测试一下。又简单有快。
测试一下,你就能确定中间变量和你的是不是一样,你是不是可以照着他的方法弄
1、框架或者插件遇到不懂的语句而又一定要用怎么办?
测试,打印中间变量,就比较轻松了
2、单文件上传和多文件上传混合在一起的上传怎么解决?
单文件上传和多文件上传混合在一起(循环解决),多文件上传部分单独处理(选择结构)
照着手册整,可以省好多事
不懂的语句打印中间结果
1 //2、获取传入的文件数据
2 foreach ($_FILES as $key=>$val){ 3 if($_FILES[$key]['tmp_name']){ 4 // 获取表单上传文件 例如上传了001.jpg 5 $file = request()->file($key); 6 if($key=='a_content_pic3'){//多图上传的情况 7 //不用拆,官方文档有怎么用 8 $files=$file; 9 $data[$key]=null; 10 foreach($files as $perFile){ 11 // 移动到框架应用根目录/public/uploads/ 目录下 12 $info = $perFile->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 13 $data[$key].='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName().','; 14 } 15 }else{ 16 // 移动到框架应用根目录/public/uploads/ 目录下 17 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 18 // 已经上传成功,我们要把文件的路径写进数据库 19 $data[$key]='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName(); 20 } 21 } 22 }
二、thinkphp5多文件上传如何实现
1、多文件上传代码
如果你使用的是多文件上传表单,例如:
<form action="/index/index/upload" enctype="multipart/form-data" method="post">
<input type="file" name="image[]" /> <br> <input type="file" name="image[]" /> <br> <input type="file" name="image[]" /> <br> <input type="submit" value="上传" /> form>
控制器代码可以改成:
public function upload(){ // 获取表单上传文件 $files = request()->file('image'); foreach($files as $file){ // 移动到框架应用根目录/public/uploads/ 目录下 $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if($info){ // 成功上传后 获取上传信息 // 输出 jpg echo $info->getExtension(); // 输出 42a79759f284b767dfcb2a0197904287.jpg echo $info->getFilename(); }else{ // 上传失败获取错误信息 echo $file->getError(); } } }
上传界面如下:
2、上传过程中dump($_FILES);的值如下
3、上传成功的返回信息如下
其实也就是下面代码的输出结果
echo $info->getExtension(); echo "
"; // 输出 42a79759f284b767dfcb2a0197904287.jpg echo $info->getFilename(); echo "
";
三、复杂结构数据的多文件上传
1、上传过程中部分dump($_FILES);的值如下
结构特点是单文件上传和多文件上传混合在一起(循环解决),多文件上传部分单独处理(选择结构)
2、核心代码
1 //2、获取传入的文件数据 2 foreach ($_FILES as $key=>$val){ 3 if($_FILES[$key]['tmp_name']){ 4 // 获取表单上传文件 例如上传了001.jpg 5 $file = request()->file($key); 6 if($key=='a_content_pic3'){//多图上传的情况 7 //不用拆,官方文档有怎么用 8 $files=$file; 9 $data[$key]=null; 10 foreach($files as $perFile){ 11 // 移动到框架应用根目录/public/uploads/ 目录下 12 $info = $perFile->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 13 $data[$key].='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName().','; 14 } 15 }else{ 16 // 移动到框架应用根目录/public/uploads/ 目录下 17 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 18 // 已经上传成功,我们要把文件的路径写进数据库 19 $data[$key]='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName(); 20 } 21 } 22 }
3、上传好了的结果数据
1 array(11) { 2 ["a_type"] => string(1) "2" 3 ["a_title"] => string(5) "33333" 4 ["a_brief"] => string(11) "33333333333" 5 ["a_content_art1"] => string(0) "" 6 ["a_create_time"] => int(1535563492) 7 ["a_update_time"] => int(1535563492) 8 ["a_authorid"] => string(2) "33" 9 ["a_g_id"] => int(17) 10 ["a_jieduan_id"] => string(1) "1" 11 ["a_picture"] => string(81) "/static/uploads/student/note/engage/20180830\dd2c9ae77b8c2c1f764d98ee86dcfb71.png" 12 ["a_content_pic3"] => string(164) "/static/uploads/student/note/engage/20180830\f972fe240bc0d315ae0e3f5fadd31486.jpg,/static/uploads/student/note/engage/20180830\71646f316808c0cd057813086c76554c.jpg," 13 }