php - 如何使文件只上传图片 - Laravel

Sah*_*nto 1 php photo image laravel

你好,我是 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'>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

    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'));
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码的视图

Sag*_*tam 5

你在前端有这样的代码:

看法

<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>
Run Code Online (Sandbox Code Playgroud)

路线

Route::post('upload/photo','TestController@uploadPhoto');
Run Code Online (Sandbox Code Playgroud)

测试控制器

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.
}
Run Code Online (Sandbox Code Playgroud)

我希望,你明白。