简体   繁体   English

不能写图像数据laravel 5干预

[英]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. 在SO中已经多次询问过这个问题,但我仍然无法弄清楚为什么这对我不起作用。

I am working on E-Commerce Laravel Project. 我正在研究电子商务Laravel项目。

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 ) 无法将图像数据写入路径( 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. 我拥有的文件夹权限是0777用于images目录及其子目录。

But still I get the above mentioned error exception. 但我仍然得到上面提到的错误异常。

EDIT 1: 编辑1:

This is the folder structure in my hosting account file manager which is inside the public_html directory: 这是我的托管帐户文件管理器中的文件夹结构,它位于public_html目录中:

文件夹结构

Can anybody help me out with this. 任何人都可以帮我解决这个问题。

Thanks in advance. 提前致谢。

You are trying to save image using url . 您正在尝试使用url保存image Instead of url try following 而不是url尝试以下

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

I have somehow figured it out. 我以某种方式想出来了。

Instead of using url() or base_path() , I used public_path() . 我没有使用url()base_path() ,而是使用了public_path()

Code that I used: 我用的代码:

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

Worked like a charm. 工作就像一个魅力。

You can not save the image using http path. 您无法使用http路径保存图像。 You have to use base_path() or manual type full server directory path /home/webtuningtechnology/public_html/images/ like this 你必须使用base_path()或手动类型的完整服务器目录路径/ home / webtuningtechnology / public_html / images /喜欢这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM