Home >Backend Development >PHP Tutorial >laravel文本表单和图片上传表单一起提交,控制器怎么处理?
下面这个控制器里面是一个示例,它的图片是可有可无的,所以是把图片单独分出来处理。现在想改一下,就是图片必须上传,那么可以把图片和文本一起$request->all()么?怎么修改代码?
<code>public function store(ArticleRequest $request) { $article = new Article($request->except('image')); $article -> user_id = Auth::id(); $picture = ""; if(Input::hasFile('image')) { $file = Input::file('image'); $filename = $file->getClientOriginalName(); $extension = $file -> getClientOriginalExtension(); $picture = sha1($filename . time()) . '.' . $extension; } $article -> picture = $picture; $article -> save(); if(Input::hasFile('image')) { $destinationPath = public_path() . '/images/article/'.$article->id.'/'; Input::file('image')->move($destinationPath, $picture); } } </code>
下面是视图:
下面这个控制器里面是一个示例,它的图片是可有可无的,所以是把图片单独分出来处理。现在想改一下,就是图片必须上传,那么可以把图片和文本一起$request->all()么?怎么修改代码?
<code>public function store(ArticleRequest $request) { $article = new Article($request->except('image')); $article -> user_id = Auth::id(); $picture = ""; if(Input::hasFile('image')) { $file = Input::file('image'); $filename = $file->getClientOriginalName(); $extension = $file -> getClientOriginalExtension(); $picture = sha1($filename . time()) . '.' . $extension; } $article -> picture = $picture; $article -> save(); if(Input::hasFile('image')) { $destinationPath = public_path() . '/images/article/'.$article->id.'/'; Input::file('image')->move($destinationPath, $picture); } } </code>
下面是视图:
做异步的上传可好?