【发布时间】:2015-02-21 11:14:03
【问题描述】:
我在 laravel 4 中为文件(图像)定义了这个验证规则。
'image' => 'image|mimes:jpeg,jpg,bmp,png,gif'
这是我的 html 代码:
{{ Form::label('Image of the product') }} {{ Form::file('image') }}
现在,即使我选择 png、jpg 图像,我在上传图像时总是会出现 2 个错误
The image must be an image.
The image must be a file of type: jpeg, jpg, bmp, png, gif.
这是包含图片上传的更新操作:
public function update($id)
{
$product = Product::find($id);
$validator = Validator::make($data = Input::all(), Product::$rules);
if($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$old_img = "";
if(isset($data['image']) and !empty($data['image']))
{
$image = Input::file('image');
$filename = time().'-'.$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);
$data['image'] = 'img/products/'.$filename;
$old_img = $product->image;
}
$product->update($data);
File::delete('public/'.$old_img);
return Redirect::route('admin.products');
}
【问题讨论】:
-
您能否提供 HTML 以防万一您在那里做错了什么。发生错误的具体步骤是什么?
-
{{ Form::label('产品图片') }} {{ Form::file('image') }}
-
你试过只使用哑剧,像这样:
'image' => 'mimes:jpeg,jpg,bmp,png,gif' -
你能告诉我们完整的控制器动作的代码吗?
-
您是否在表单的配置数组中设置了 'files' => 'true'?