簡體   English   中英

不能寫圖像數據laravel 5干預

[英]Can't write image data laravel 5 Intervention

在SO中已經多次詢問過這個問題,但我仍然無法弄清楚為什么這對我不起作用。

我正在研究電子商務Laravel項目。

問題是,當管理員上傳產品的圖像文件時,會收到以下錯誤:

無法將圖像數據寫入路徑( http://example.com/ankur/images/uploads/products/img-newprod-540-350-350.jpg

這是存儲圖像和相關條目的控制器片段:

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

我擁有的文件夾權限是0777用於images目錄及其子目錄。

但我仍然得到上面提到的錯誤異常。

編輯1:

這是我的托管帳戶文件管理器中的文件夾結構,它位於public_html目錄中:

文件夾結構

任何人都可以幫我解決這個問題。

提前致謝。

您正在嘗試使用url保存image 而不是url嘗試以下

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

我以某種方式想出來了。

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

我用的代碼:

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

工作就像一個魅力。

您無法使用http路徑保存圖像。 你必須使用base_path()或手動類型的完整服務器目錄路徑/ home / webtuningtechnology / public_html / images /喜歡這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM