我收到此错误:LogicException:在尝试上传图片时,无法猜测mime类型,因为没有可用的猜测器(您是否启用了php_fileinfo扩展名?) 我启用了php_fileinfo扩展,并重新启动了Wamp Web服务器,但我仍无法解决此问题。我错过了什么?感谢
以下是我的代码:
型号:Product.php
class Product extends Eloquent {
protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif|max:3000',
);
public function category() {
return $this->belongsTo('Category');
}
}
控制器:ProductsController.php
public function postCreate() {
$validator = Validator::make(Input::all(), Product::$rules);
if($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468,249)->save('public/img/products/'.$filename);
$product->image = 'img/products/'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message', 'Product Created');
}
return Redirect::to('admin/products/index')
->with('message', 'Something went wrong')
->withErrors($validator)
->WithInput();
}
观点:Index.blade.php
{{ Form::open(array('url'=>'admin/products/create', 'files'=>true)) }}
<p>
{{ Form::label('category_id', 'Category') }}
{{ Form::select('category_id', $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('description') }}
{{ Form::textarea('description') }}
</p>
<p>
{{ Form::label('price') }}
{{ Form::text('price', null, array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('image', 'Choose an image') }}
{{ Form::file('image') }}
</p>
{{ Form::submit('Create Product', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
答案 0 :(得分:25)
将php.ini中的这一行取消注释到php文件夹中。
延长= php_fileinfo.dll
并重新启动服务器(再次输入'php artisan serve')。这种方式有效!
答案 1 :(得分:5)
打开php.ini文件,你可以找到; extension = php_fileinfo.dll删除half-coloumn到extension = php_fileinfo.dll将完美地工作然后重新启动你的apache服务器或xampp,wampp你正在使用的任何环境
答案 2 :(得分:1)
我认为是WAMP Web服务器的Bug。我切换到XAMPP Web服务器,它工作正常。
非常感谢。