【问题标题】:file_get_contents(C:\Users\dell\Documents\accounts\gna\public/storage/img/RsNR7dEIisWrF30dhrys.jpg): failed to open stream: No such file or directoryfile_get_contents(C:\Users\dell\Documents\accounts\gna\public/storage/img/RsNR7dEIisWrF30dhrys.jpg):无法打开流:没有这样的文件或目录
【发布时间】: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);
  • 感谢试了,还是不行。

标签: php laravel


【解决方案1】:

上面的问题我解决了如下;

 public function change()
    {
        #Get auth user
        $user = auth()->user();

        //get img and save
        $avatarFile = $this->avatar->store('img');
        //get ext
        $type = pathinfo($avatarFile, PATHINFO_EXTENSION);
        //Convert to base64
        $avatarBase64 = "data:image/$type;base64," . base64_encode(Storage::get($avatarFile));
        //Delete the product image from the server as it is no longer needed
        Storage::delete($avatarFile);

        //Store base64
        $user->avatar = $avatarBase64;
        $user->save();
        session()->flash('success', 'Avatar successfully changed!');
        return redirect()->route('settings');
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 2017-06-20
    • 2017-05-04
    • 2020-01-12
    • 2018-09-20
    相关资源
    最近更新 更多