【问题标题】:Laravel can't upload file in storage folderLaravel 无法上传存储文件夹中的文件
【发布时间】:2018-09-10 03:31:24
【问题描述】:

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

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

错误来自这一行(我使用intervention/image包)

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);
}

【问题讨论】:

  • 试试这个$location = storage_path('/images/' . $filename);
  • @Nawin 仍在存储文件夹中上传:/
  • 你的存储路径是什么返回并打印你的$location变量
  • 我如何获得打印的内容?
  • 给我C:\laragon\www\mynewsite\storage\imagesaboutimage-1522483434.png"

标签: php laravel laravel-storage


【解决方案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在更新方法中删除它们的路径。

【讨论】:

    猜你喜欢
    • 2021-04-27
    • 2017-09-05
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2018-02-16
    相关资源
    最近更新 更多