php - 错误消息:在 null 上调用成员函数 getRealPath()
问题描述
我试图在 Heroku 上发布图像。但是图像无法生成,也没有显示在 Heroku 上。当我发布时,图像会显示在本地主机上。
一篇文章告诉我,它可以用base64显示。请帮忙。
帖子控制器
public function store(PostRequest $request)
{
$image_file = "";
$upload_image = $request->file('image_file');
$post->image = base64_encode(file_get_contents($request->image->getRealPath())); //here is error
if ($upload_image) {
$path = $upload_image->store('uploads', 'public');
if ($path) {
Image::make($upload_image->getRealPath())->resize(150, 150)->save();
$image_file = $path;
}
}
$post = new Post;
$post->subject = $request->subject;
$post->message = $request->message;
$post->image_file = $image_file;
$post->name = "";
$post->user_id = Auth::id();
$post->save();
return redirect('/bbs')->with('poststatus', '');
}
索引刀片
<td>
@if (empty($post->image_file))
@else
<img src="data:image/png;base64,<?= $post->image ?>" width="150px">
@endif
</td>
解决方案
推荐阅读
- javascript - 如果我已经定义了 div,定义了它的类、id 等,我可以稍后(以某种方式)添加 onclick="myFunction()" 吗?
- python-3.x - /save_data/ 处的 ValueError
- ios - MessageKit 中的 messageCollectionView 无法识别
- python - Python默认参数分配列表的长度
- heroku - 部署了heroku应用程序,但是当我尝试访问该应用程序时,当我使用heroku日志时出现以下错误--tail
- javascript - 刚才在 VueJS 和 Laravel
- tensorflow - 在线训练有状态的深度 LSTM 堆栈时出错
- javascript - 是否可以触发注入