如何解决错误:在Laravel中调用成员函数getPathName()在null上?

时间:2019-09-13 07:40:05

标签: php laravel

我想从我的网站上传视频,同时上传youtube。

这是代码HTML

UITableView

下面是上传视频的功能

class MajorDatasource: NSObject, UITableViewDataSource {
    var minorDatasources:[MinorDatasource]

    init(minorDatasources:[MinorDatasource]) {
        self.minorDatasources = minorDatasources
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return minorDatasources.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MajorTableViewCell")! as! MajorTableViewCell
        cell.tableView.dataSource = minorDatasources[indexPath.row]
        return cell
    }
}

class MinorDatasource: NSObject, UITableViewDataSource {
    var labels:[String]

    init(labels:[String]) {
        self. labels = labels
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return labels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MinorTableViewCell")!
        cell.textLabel?.text = labels[indexPath.row]
        return cell
    }
}

我测试时,出现错误:

  

在null上调用成员函数getPathName()

1 个答案:

答案 0 :(得分:0)

因此,在您的表单中,您缺少允许文件上传的enctype

<form action="" method="post" class="form-horizontal form-simple" enctype="multipart/form-data">

当然还有操作

问题是您在控制器中的$request->file('video')null,因此是异常的原因。

因此,最好始终先检查是否有文件,然后再继续上传,并添加一个保护措施,例如:

if ( ! $request->hasFile('video') )
{
    return back()->withError('No file uploaded');
}

或者如果hasFile由于某种原因总是返回false,只需将其替换为$request->file('video')即可,返回null(如果不是视频),因此再次进行检查。