我創建了一個客戶端的網站與我遇到這個錯誤圖像等Laravel:調用一個成員函數getRealPath()非對象
畫廊上:
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'=>布爾值false private'originalName'=> string'ClapSlide.png'(length = 13) private'mimeType '=> string'image/png'(length = 9) private'size'=> int 0 private'error'=> int 0'已經給出了整個存儲函數 – Grey
很高興你解決了你的問題,通過使用laravel上傳文件的方式有更好的解決方法,而不是創建一個新的圖像,你可以移動你上傳的文件。 –