Home >Backend Development >PHP Tutorial >laravel文本表单和图片上传表单一起提交,控制器怎么处理?

laravel文本表单和图片上传表单一起提交,控制器怎么处理?

WBOY
WBOYOriginal
2016-06-06 20:24:201344browse

下面这个控制器里面是一个示例,它的图片是可有可无的,所以是把图片单独分出来处理。现在想改一下,就是图片必须上传,那么可以把图片和文本一起$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>

下面是视图:
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>

下面是视图:
laravel文本表单和图片上传表单一起提交,控制器怎么处理?

做异步的上传可好?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn