【问题标题】:Laravel saves temp file as imageLaravel 将临时文件保存为图像
【发布时间】:2018-05-23 05:33:35
【问题描述】:

我的产品有 3 个图像字段,保存方法一切正常,但更新方法 first image 将临时文件保存在数据库中,其他两个更新没有问题。

代码

我在这里分享我所有 3 张图片的更新方法:

//saves temp file instead of file name
if ($request->hasFile('imageOne')) {
            $imageOne = $request->file('imageOne');
            $filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageOne)->resize(1200, 600)->save($location);

            if(!empty($product->imageOne)){
                Storage::delete('images/' . $product->imageOne);
            }
            $product->imageOne = $imageOne;            
        }
//works with no issue
        if ($request->hasFile('imageTwo')) {
            $imageTwo = $request->file('imageTwo');
            $filename = 'producttwo' . '-' . time() . '.' . $imageTwo->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageTwo)->resize(1200, 600)->save($location);

            if(!empty($product->imageTwo)){
                Storage::delete('images/' . $product->imageTwo);
            }
            $product->imageTwo = $filename;            
        }
//works with no issue
        if ($request->hasFile('imageThree')) {
            $imageThree = $request->file('imageThree');
            $filename = 'productthree' . '-' . time() . '.' . $imageThree->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageThree)->resize(1200, 600)->save($location);

            if(!empty($product->imageThree)){
                Storage::delete('images/' . $product->imageThree);
            }
            $product->imageThree = $filename;            
        }

有什么想法吗?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    问题在这里:-

    $product->imageOne = $imageOne; 
    

    您正在保存 $imageOne。但是你需要保存$filename。使用以下代码:-

    $product->imageOne = $filename;
    

    【讨论】:

    • 祝福你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多