[英]"Can't write image data to path" - Laravel Image Intervention
[英]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.