D
D
dima_maketov2017-03-27 14:00:32
Laravel
dima_maketov, 2017-03-27 14:00:32

Laravel 5.4. How to upload files to the site?

Hello!
How do you implement file uploads, and why does the script not see the files that I want to upload to the site?

$files = Input::file('images'); 
dd($files); // возращает null

create.blade.php
<form action="carts" method="post">
      {{ csrf_field() }}
      <div class="form-group">
          <label for="name" class="col-md-4 control-label">ФИО клиента</label>
          <div class="col-md-6">
              <input name="name" type="text" id="name" class="form-control" required autofocus>
          </div>
      </div>
      <div class="form-group">
          <label for="url" class="col-md-4 control-label">Url страницы</label>
          <div class="col-md-6">
              <span>{{ config('app.url') }}/{{$random}}</span>
              <input id="url" type="hidden" class="form-control" name="url" value="{{$random}}" required>
          </div>
      </div>
      <div class="form-group">
        <label for="image" class="col-md-4 control-label">Фотографии</label>
        <div class="col-md-6">
            <input type="file" name="images" multiple>
        </div>
      </div>
      <div class="form-group">
          <div class="col-md-8 col-md-offset-4">
              <button type="submit" class="btn btn-primary">
                  Создать
              </button>
          </div>
      </div>
  </form>

Controller
public function store(Request $request)
    {
      PersonalCart::create([
        'name_user' => $request->name,
        'url_cart' => $request->url
      ]);
      if (Input::hasFile('images'))
      {
        $files = Input::file('images');

        foreach($files as $file) {
           $destinationPath = public_path() .'/uploads/';
           $filename = $file->getClientOriginalName();
           $file->move($destinationPath, $filename);
        }
      }
    }

web.php
Route::get('/', '[email protected]');
Route::post('/carts', '[email protected]');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2017-03-27
@dima_maketov

enctype="multipart/form-data"the form tag should have

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question