干预\Image\Exception\imagenotwriteableexception使用Laravel 4
我用的是Laravel4。我不知道为什么我会在一切似乎都是正确的情况下出现这个错误。此外,产品未更新到数据库 错误:干预\Image\Exception\ImageNotWritableException 无法将图像数据写入路径[/img/products/1396668877.jpg] 创建产品对象的ProductsController片段:干预\Image\Exception\imagenotwriteableexception使用Laravel 4,image,upload,laravel,Image,Upload,Laravel,我用的是Laravel4。我不知道为什么我会在一切似乎都是正确的情况下出现这个错误。此外,产品未更新到数据库 错误:干预\Image\Exception\ImageNotWritableException 无法将图像数据写入路径[/img/products/1396668877.jpg] 创建产品对象的ProductsController片段: public function postCreate() { $validator = Validator::make(Input::all()
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 = time() . '.' . $image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize(468, 249)->save('/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();
}
传递到视图的产品对象
确保
public/img/products
文件夹存在且可写,如有必要,也尝试使用绝对路径,如下所示:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
替换:
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
与:
您必须为
save
方法指定public
文件夹。工作正常!谢谢为什么绝对路径比相对路径有效。是因为它在本地服务器上吗?
public function up()
{
Schema::create('products', function($table){
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string('title');
$table->text('description');
$table->decimal('price', 6, 2);
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
});
}
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);