首页 > 解决方案 > 错误调用 bool 上的成员函数 getRealPath()

问题描述

我收到此错误“调用 bool 上的成员函数 getRealPath()”

if($request->hasFile('content')) { 
    $filenameWithExt = $request->file('content')->getClientOriginalName(); 
    $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME); 
    $extension = $request->file('content')->getClientOriginalExtension(); 
    $fileNameToStore = $filename.'_'.time().'.'.$extension; 
    $path = $request->file('content')->storeAs('public/content',$fileNameToStore); 
} else { 
    $fileNameToStore = 'No Image,Music and Video selected please! check and try again.'; 
}
$post = new Post;
 $post->body = $request->input('body');
 $post->content = $fileNameToStore;
 //Error exist here
 $post = Image::make($fileNameToStore->getRealPath());
 $post->text('The quick brown fox jumps over the lazy dog.');
 $post->save();

标签: laravel

解决方案


getRealPath()是方法SplFileInfo

见:https ://www.php.net/manual/en/splfileinfo.getrealpath.php

如果你想使用getRealPath(),试试这个:

Image::make(
    $request->file('content')->getRealPath()
)->save('public/content',$fileNameToStore);

推荐阅读