File Upload Redirect with Error Not Display

- 1 answer

Ad

I'm not sure what is the cause of my file upload. It's failing and redirecting back, but not with error.


Controller

public function updateLogo()
{

    $inputs = Input::all();
    $logo_path = Input::only('logo_path');
    $cpe_mac = $inputs['cpe_mac'];
    $rule =  ['logo_path' => 'mimes:jpeg,bmp,png'];

    $validator = Validator::make($logo_path, $rule );

    if ( $validator->fails()) {
        return Redirect::to($cpe_mac.'/view-profile/')->withErrors($validator)->withInput();
    } else {

    ..... 

    }

View

{!! Form::open(array('url' => '/'.$cpe_mac.'/view-profile/logo/update', 'class' => 'form-horizontal', 'role' =>'form','id' => 'editLogo','file'=>true)) !!}

  <input name="logo_path" type="file" required> <br><br>

   <button class="btn btn-success btn-sm mr5" type="file"><i class="fa fa-user"></i> Update Logo</button>

{!! Form::hidden('cpe_mac', $cpe_mac)!!}
{{ csrf_field() }}
{!! Form::close();!!}

Did I forget something ?

Ad

Answer

Ad

You need to change your validation slightly in order to get this to work to follow up on @marmorunl answer.

public function updateLogo()
{
    $input = [
        'logo_path' => Input::file('logo_path')
    ];

    $rules = [
        'logo_path' => 'mimes:jpeg,bmp,png|required'
    ];

    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {
         return Redirect::to($cpe_mac.'/view-profile/')->withErrors($validator)->withInput()
    } else {
        // else
    }
};

See Laravel: Validate an uploaded file is an image for additional information. This suggests using image as the validation rule which would also include git and svg. So I've left it as the mime type one you where using in case you don't want this.

Ad
source: stackoverflow.com
Ad