tp多文件上传致命错误: Call to a member function move() on string

在这里插入图片描述

解决方案

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

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

<?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视图
在这里插入图片描述
错误解析:

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

正确写法:

<!DOCTYPE html>
<html lang="zh">
<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>

或者(推荐使用):

<!DOCTYPE html>
<html lang="zh">
<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:可在一个上传控件里上传多个文件。比较节省代码。

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/87875391
今日推荐