无法从存储加载图像(Laravel)-> 加载资源失败:服务器响应状态为 404(未找到)

问题描述 投票:0回答:1

大家好,我遇到了这个问题,我无法显示存储文件夹中的图像。 我已经使用符号链接 (

php artisan storage:link
) 将存储文件夹连接到公共文件夹,但每次我尝试可视化它时,我都会从控制台收到此错误:
"Failed to load resource: the server responded with a status of 404 (Not Found)"

我不知道这是否有用,但这是我的 ProductController 的存储功能:

public function store(ProductRequest $request)
    {
        $user = Auth::user();
        $category = Category::find($request->category);
        $subcategory = Subcategory::find($request->subcategory);
        $product = new Product();
        $product->name = $request->name;
        $product->availability = $request->availability;
        $product->slug = Str::slug($request->name, '-', 'it').'-'.bin2hex(random_bytes(4));
        $product->description = $request->description;
        $product->price = $request->price;
        $product->category()->associate($category);
        $product->subcategory()->associate($subcategory);
        
        $product->save();
        $uniqueSecret = $request->uniqueSecret;
        $images = session()->get("images.{$uniqueSecret}", []);
        $removedImages = session()->get("removedImages.{$uniqueSecret}", []);
        $images = array_diff($images, $removedImages);

        foreach ($images as $image)
        {
            $newImage = new ProductImage();
            $fileName = basename($image);
            $newFileName = "/public/products/{$product->id}/{$fileName}";
            Storage::move($image, $newFileName);
            $newImage->file = $newFileName;
            $newImage->product_id = $product->id;
            $newImage->save();
        }
        File::deleteDirectory(storage_path("/app/public/temp/{$uniqueSecret}"));

        return redirect(route('product.create'))
    }

这里是我回忆起图像的视图部分的代码:

@foreach ($product->images as $image)
                    
   <div class="carousel-item">
      <img class="d-block w-100" src="{{ asset($image['file'])}}" >
   </div>
 @endforeach

dd($image['file']
我得到:

/public/products/21/vZIxZfm6G8vDgmTv2zfW0aFSbotdnQ4UzWR59oXL.png

我还尝试在公共和产品之间添加存储,但它只会在存储/应用程序中创建一个名为 storage 的新文件夹

(也许有用:我正在使用 dropzone 直接从网站上传图像,显然上传过程运行良好)

不知道如何解决这个问题,希望有人能帮助我,谢谢

php laravel file-upload local-storage dropzone
1个回答
0
投票

这样做

 $imageName = time().'.'.$image->extension();  
 $image->move(public_path('products'), $imageName);
    

并且在获取时

<img src="{{ asset('products/'.$imageName) }}" alt="Uploaded Image">
© www.soinside.com 2019 - 2024. All rights reserved.