【发布时间】:2022-07-31 17:05:03
【问题描述】:
我是 Laravel 框架的新手。我按照我在教程中看到的内容编写了这段代码。我尝试更改图像上传路径,但仍然得到相同的结果。请问这个问题怎么解决?
public function change()
{
#Get auth user
$user = auth()->user();
$avatar = $this->avatar->store('img'); #Save avatar image
$path = $_SERVER['DOCUMENT_ROOT']."/storage/$avatar"; #Take the avatar's path
$type = pathinfo($path, PATHINFO_EXTENSION); #Get avatar image type
$image = file_get_contents($path); #Get the avatar image
$avatarBase64 = "data:image/$type;base64,".base64_encode($image); #Convert avatar image to base64
Storage::delete($avatar); #Delete the avatar image from the server as it is no longer needed
$user->avatar = $avatarBase64;
$user->save();
session()->flash('success', 'Avatar successfully changed!');
return redirect()->route('settings');
}
【问题讨论】:
-
将
storage_path()用于您的$path变量:$path = storage_path($avatar); -
感谢试了,还是不行。