这个问题已经在 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
目录内:
任何人都可以帮我解决这个问题。
提前致谢。