php - 如何使文件仅上传图像 - Laravel

时间:2017-09-04 02:42:38

标签: php image laravel photo

你好我在laravel的新手所以我真的需要一些帮助。我想创建一个代码,其中只有可以上传其他文件的图片不能,我已经尝试使用代码输入文件,但是当我尝试上传zip文件时,它仍然上传,所以我真的需要帮助

这是我的表格代码

<div class="col-sm-5">
{!! Form::label('photo', 'Photo:') !!}
<input type='file' name='photo' class='form-control' accept = 'image/jpeg , image/jpg, image/gif, image/png'>

这是我的控制器

    public function store(CreateBannerRequest $request)
{

    $input = $request->all();
    //get original file name
    if($request->photo == NULL)
    {
        Flash::error('Image must be filled');
        return back();
    }
    $filename = Input::file('photo')->getClientOriginalName();
    $input['photo'] =  $filename;
     $banner = $this->BannerRepository->create($input);
    //upload file
    Input::file('photo')->move($this->path, $filename); 

     Flash::success('Banner saved successfully.');

     if (empty($banner)) {
        Flash::error('No image available');

        return redirect(route('banner.index'));
    }

     return redirect(route('banner.index'));
}

This is the view of my code

2 个答案:

答案 0 :(得分:3)

你的前端代码如下:

查看

<form action="{{URL::to('upload/photo')}}" class="form-horizontal" method="POST" role="form" enctype="multipart/form-data">
    <input type="file" name="photo">
    <button class="btn btn-default pull-right" type="submit">Create</button>
</form>

<强>路线

Route::post('upload/photo','TestController@uploadPhoto');

<强>的TestController

public function uploadPhoto(Request $request)
{
    $this->validate($request, [
        'photo' => 'mimes:jpeg,png,bmp,tiff |max:4096',
    ],
        $messages = [
            'required' => 'The :attribute field is required.',
            'mimes' => 'Only jpeg, png, bmp,tiff are allowed.'
        ]
    );
 // Now save your file to the storage and file details at database.
}

我希望,你不准。

答案 1 :(得分:0)

您可以通过mimes:jpeg验证以及其他类型(例如png等)在文档页面上查找laravel验证来进行验证