【问题标题】:Can't write image data laravel 5 Intervention无法写入图像数据 laravel 5 干预
【发布时间】:2015-09-23 04:34:09
【问题描述】:

这个问题已经在 SO 中被问过很多次,但我仍然无法弄清楚为什么这对我不起作用。

我正在从事电子商务 Laravel 项目。

问题是,当管理员上传产品的图片文件时,得到如下错误:

无法将图像数据写入路径 (http://example.com/ankur/images/uploads/products/img-newprod-540-350-350.jpg)

这是存储图像和相关条目的控制器sn-p:

public function storeGeneral(Request $request)
{

    $this->validate($request, [
    'code'                => 'required|alpha_dash|unique:products',
    'name'                => 'required',
    'description'         => 'string',
    'details'             => 'string',
    'price'               => 'required|regex:/^\d*(\.\d{2})?$/',
    'tax_amount'          => 'required|regex:/^\d*(\.\d{2})?$/',
    'net_price'           => 'required|regex:/^\d*(\.\d{2})?$/',
    'weight'              => 'required|integer',
    'image_file'          => 'image|mimes:jpeg,jpg,JPG'
    ]);
    if ($request->ajax() ) {
        if ($request->file('image_file' ) ) {
            $request['img_code'] = 'img-' . $request->get('code' );
            $product = Product::create($request->all() );
            $product->categories()->attach($request->input('category_id') );

            Session::put('product_last_inserted_id', $product->id);

            $mgCode = DB::table('products')->latest()->limit(1)->pluck('img_code');
            $imageType = [
                'product' => [
                    'height' => 350,
                    'width' => 350
                ],
                'carousel' => [
                    'height' => 163,
                    'width' => 163
                ],
                'cart' => [
                    'height' => 64,
                    'width' => 64
                ]
            ];

            foreach($imageType as $key => $value)
            {
                $fileName = Safeurl::make($mgCode );
                $image = Image::make($request->file('image_file' ) );
                //$path = public_path( 'images/uploads/products/' );
                $path = url('/images/uploads/products/');

                if ($key == 'product') {
                    $image->resize($value['width'], $value['height'] );
                    $image->save($path.'/'.$fileName."-". $value['width'] ."-".$value['height'] .".jpg", 100 );
                } else if ($key == 'carousel' ) {
                    $image->resize($value['width'], $value['height'] );
                    $image->save($path.'/'.$fileName."-". $value['width'] ."-".$value['height'] .".jpg", 100 );
                } else if ($key == 'cart' ) {
                    $image->resize($value['width'], $value['height'] );
                    $image->save($path.'/'.$fileName."-". $value['width'] ."-".$value['height'] .".jpg", 100 );
                }
            }
        } else {
            $product = Product::create($request->all() );
            Session::put('product_last_inserted_id', $product->id);
        }
        return response(['status' => 'success', 'msg' => 'The product has been added successfully.']);
    }
    return response(['status' => 'failed', 'msg' => 'The product could not be added successfully.']);
}

对于images 目录及其子目录,我拥有的文件夹权限是0777

但我仍然收到上述错误异常。

编辑 1:

这是public_html 目录内的托管帐户文件管理器中的文件夹结构:

谁能帮帮我。

提前致谢。

【问题讨论】:

  • 您是否已经创建了此路径? /图片/上传/产品/
  • 是的..我已经创建了目录。

标签: php image laravel-5 intervention


【解决方案1】:

我想通了。

我没有使用url()base_path(),而是使用了public_path()

我使用的代码:

$path = public_path('../../public_html/ankur/images/uploads/products');

工作就像一个魅力。

【讨论】:

    【解决方案2】:

    您正在尝试使用url 保存image。而不是url 尝试关注

    $path = base_path().'/images/uploads/products';
    

    【讨论】:

    • 不。得到以下错误:Can't write image data to path (/home/benefitho/ankur-laravel/images/uploads/products//img-newprod-540-150420-350-350.jpg)
    • 你可以尝试从“产品”前面删除正斜杠
    【解决方案3】:

    您不能使用 http 路径保存图像。您必须像这样使用 base_path() 或手动输入完整的服务器目录路径 /home/webtuningtechnology/public_html/images/

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2016-01-11
      相关资源
      最近更新 更多