MultiFile Upload Validation in Laravel (v4.2)

  • view
<!-- uploaded -->
@if (Session::has('done'))
  @foreach (Session::get('done') as $yes)
      <li>{{ $yes }}</li>
  @endforeach
  <p style="color: #2ecc71">Uploaded</p>
  <br>
@endif

<!-- not uploaded -->
@if (Session::has('not'))
  @foreach (Session::get('not') as $no)
    <li>{{ $no }}</li>
  @endforeach
  <p style="color: #c0392b">wasnt uploaded</p>
  <br>
@endif

<hr>

<!-- form -->
{{ Form::open(['url' => 'up', 'files'=>true]) }}
  {{ Form::file('images[]', ['multiple'=>true]) }}
  {{ Form::submit('Upload') }}
{{ Form::close() }}
  • routes
Route::get('/', function() {
  return View::make('main');
});
Route::post('up', 'FUplaodController@store');
  • controller
public function store()
{
  if (Input::hasFile('images'))
  {
    $files = Input::file('images');
    $rules = [
        'file' => 'required|image'
    ];
    $destinationPath = public_path().'/uploads';

    foreach ($files as $one)
    {
      $v = Validator::make(['file' => $one], $rules);
      if ($v->passes())
      {
        $filename       = $one->getClientOriginalName();
        $upload_success = $one->move($destinationPath, $filename);
        if ($upload_success)
        {
          $done[] = $filename;
          Session::flash('done', $done);
        }
      } else {
        $filename = $one->getClientOriginalName();
        $not[] = $filename;
        Session::flash('not', $not);
      }
    }
    return Redirect::back()->withErrors($v);
  }
  return Redirect::back()->withErrors('choose a file');
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s