【问题标题】:Laravel: Call to a member function getRealPath() on a non-object [closed]Laravel:在非对象上调用成员函数 getRealPath() [关闭]
【发布时间】:2016-07-18 14:17:09
【问题描述】:

我正在为客户创建一个网站,其中包含图片库等。

我遇到了这个错误:

ImageController Line 16: Call to a member function getRealPath() on a non-object

ImageController 看起来像这样:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Image;
use Illuminate\Http\Request;

class ImageController extends Controller {

    public function __construct() {

}

public function store(Request $request) {
    $file = $request->file('image')->getRealPath();
    $image = new Image();
    $image->title = $request->get('title');
    $image->desc = $request->get('desc');
    //$image->date = $request->get('created_at');
    $image->image = base64_encode(file_get_contents($file));
    $image->type = pathinfo($file, PATHINFO_EXTENSION);
    if($image->save()) {
        return redirect(route('web.home'))->with('status', 'Uploaded!');
    } else {
        return redirect(route('store.upload'))->with('status', 'Upload failed!')->withInput();
    }
}

}

我的表单如下所示:

<form action="{{ url(route('store.upload')) }}" method="post" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text" id="title" name="title">
    <label class="mdl-textfield__label" for="title">Titel</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
    <textarea class="mdl-textfield__input" type="text" rows="5" id="desc"></textarea>
    <label class="mdl-textfield__label" for="desc">Beschrijving</label>
</div>
<input type="file" name="image">
<button class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored right" type="submit">
    <i class="material-icons">add</i>
</button>

这基本上是对我创建的早期画廊的编辑,应该(理论上)工作。

【问题讨论】:

  • 你能评论函数存储中的所有内容并发布 var_dump($request->file('image')) 的结果吗? ;
  • @NicolòCozzani object(Symfony\Component\HttpFoundation\File\UploadedFile)[29] private 'test' =&gt; boolean false private 'originalName' =&gt; string 'ClapSlide.png' (length=13) private 'mimeType' =&gt; string 'image/png' (length=9) private 'size' =&gt; int 0 private 'error' =&gt; int 0整个店铺功能已经给了
  • 很高兴您解决了问题,顺便说一句,使用 laravel 上传文件有更好的解决方案,而是创建一个新图像,您只需移动上传的文件即可。
  • 感谢您的额外解决方案,虽然我现在更喜欢这样做,但并不是每天都会上传大量图片,这个网站的所有者只是会使用它用于简单的小型企业目的

标签: php forms laravel


【解决方案1】:

好的,我发现了问题,我声明了其中一个路由错误,之后错误消失了,我得到另一个错误,这是因为我忘记在表中添加文件类型列,之后我忘记了为描述设置名称,这样也会导致错误,现在每个错误都已修复并且可以正常工作

【讨论】:

  • 那么这个问题就多余了;这个答案对其他有类似问题的用户一点用处都没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多