Search code examples
phpimagelaravel-5intervention

Can't write image data laravel 5 Intervention


This question have been asked many times in SO, but still I cannot figure it out why is this not working for me.

I am working on E-Commerce Laravel Project.

The issue is that, when the admin uploads the image file of the product, get the error of:

Can't write image data to path (http://example.com/ankur/images/uploads/products/img-newprod-540-350-350.jpg)

Here's the controller snippet of storing the image and related entries:

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.']);
}

The folder permission that I have is 0777 for the images directory and it's sub directories.

But still I get the above mentioned error exception.

EDIT 1:

This is the folder structure in my hosting account file manager which is inside the public_html directory:

Folder structure

Can anybody help me out with this.

Thanks in advance.


Solution

  • I have somehow figured it out.

    Instead of using url() or base_path(), I used public_path().

    Code that I used:

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

    Worked like a charm.