1

我曾经将上传文件(图像)存储在我的公用文件夹中,但这次我想将它们存储在我的存储文件夹中,但出现此错误

Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)

错误来自这一行(我使用干预/图像包的地方)

Image::make($image)->resize(1200, 600)->save($location);

我的功能:

if ($request->hasFile('image')) {
        $image = $request->file('image');
        $filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
        $location = storage_path('images/' . $filename);
        Image::make($image)->resize(1200, 600)->save($location);

        $oldFilename = $indexabout->image;
        $indexabout->image = $filename;
        Storage::delete($oldFilename);
}

任何想法?

更新

我的文件将上传到root/storage文件夹而不是root/storage/app/public/images

这是为什么?

更新 2

我将我的函数更改为下面的代码,它正在上传它应该上传的地方,但是it does not delete the old image

if ($request->hasFile('image')) {
            $image = $request->file('image');
            $filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
            $location = storage_path('app/public/images' . $filename);
            Image::make($image)->resize(1200, 600)->save($location);

            $oldFilename = $indexabout->image;
            $indexabout->image = $filename;
            Storage::delete($oldFilename);
}
4

1 回答 1

1

解决了

这是最终代码:

if ($request->hasFile('image')) {
            $image = $request->file('image');
            $filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename); // root storage path
            Image::make($image)->resize(1200, 600)->save($location);

            Storage::delete('images/' . $indexabout->image); //public storage path
            $indexabout->image = $filename;            
        }

基本上我给出Root/Storage了存储数据的Public/Storage路径和在更新方法中删除它们的路径。

于 2018-03-31T08:46:16.607 回答