本文作者:DurkBlue

thinkphp在进行多文件上传出现致命错误: Call to a member function move() on string

thinkphp在进行多文件上传出现致命错误: Call to a member function move() on string摘要: 解决方案在使用tp上传多个文件上传的时候,一定要注意前台的写法!!!先把我代码贴出来参考一下:index控制器:1234567891011121314151617181920212...

thinkphp在进行多文件上传出现致命错误: Call to a member function move() on string 第1张

解决方案

在使用tp上传多个文件上传的时候,一定要注意前台的写法!!!

先把我代码贴出来参考一下:
index控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace app\index\controller;
use think\Controller;
use think\Loader;
use think\Db;
use think\Request;
class Index extends Controller
{
    public function index()
    {
        if(isset($_POST['sub']))
        {
            $file = request()->file('file');
 
            foreach($file as $files)
            {
                $info $files->move(ROOT_PATH . 'public' . DS . 'uploads/images');
                if($info){
                echo '文件上传成功'.'<br/>';
                }
                else{
                    echo '文件上传失败'.'<br/>';
                }
            }  
         }
         else
         {
             return view('index');
         }
    }
}


index.html视图

thinkphp在进行多文件上传出现致命错误: Call to a member function move() on string 第2张


错误解析:

多文件上传的情况下,前台的name属性的值必须是一个数组格式,如果接受过来的文件是一个字符串格式,所以就无法使用foreach,从而报错。

正确写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" /><br>
    <input type="file" name="file[]" /><br>
    <input type="file" name="file[]" /><br>
    <input type="submit" name="sub" value="提交" />
</form>
</body>
</html>

或者(推荐使用):

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" multiple="multiple" /><br>
    <input type="submit" name="sub" value="提交" />
</form>
</body>
</html>

mulitple:可在一个上传控件里上传多个文件。比较节省代码。


此篇文章由DurkBlue博主亲自发布,转载请注明来处哟
文章投稿或转载声明

来源:DurkBlue版权归原作者所有,转载请保留出处。本站文章发布于 2019-11-27
温馨提示:文章内容系作者个人观点,不代表DurkBlue博客对其观点赞同或支持。

赞(0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论取消回复

快捷回复:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog

评论列表 (暂无评论,1883人围观)参与讨论

还没有评论,来说两句吧...