【问题标题】:Error Call to a member function getRealPath() on bool错误调用 bool 上的成员函数 getRealPath()
【发布时间】:2019-11-20 10:05:08
【问题描述】:

我收到此错误“调用 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();

【问题讨论】:

  • $fileNameToStoredefined 在哪里?
  • 你能发布你的 $fileNameToStore 定义的代码吗?
  • 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 = '请不要选择图片、音乐和视频!检查并再试一次。'; }
  • 您的 $fileNameToStore 似乎是字符串,而不是布尔值,字符串也没有方法 getRealPath(),您是否错过了什么?
  • 我该如何解决?

标签: laravel


【解决方案1】:

getRealPath()SplFileInfo的方法

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多