0

I'm trying to upload a file to Laravel, but I can't do it. When I do a $request->images I get a string with the name of my file. I also made several unsuccessful tests like below:

My view:

<input type="file" name="images" class="form-control">
  @if ($errors->has('images'))
    <span class="invalid-feedback"> {{ $errors->first('images') }} </span>
    @endif

  <button type="submit" class="btn btn-primary">Envoyez l'annonce !</button>

My controller:

public function store(Request $request)
    {
        // $this->validate($request, [
        //     'title' => ['required', 'string', 'max:32'],
        //     'description' => ['required', 'string'],
        //     'price' => ['required'],
        //     'localisation' => ['required'],
        //     'images' => ['required', 'image']
        // ]);
        //dd($request->input('images'));
        if ($request->hasFile('images')) {
            dd('File ok');
        } else {
            dd("File not ok");
        }
        $image= $request->file('images');
        $imageName= Str::random(40);
        $image->move('images/products', $imageName);

        Ad::create([
            'title' => $request->title,
            'description' => $request->description,
            'price' => $request->price,
            'localisation' => $request->localisation,
            'images' => $imageName
        ]);

        return back()->with('success', 'Félicitation, votre annonce à bien été postée.');
    }

The result is always the same: File not ok.

dolor3sh4ze
  • 925
  • 1
  • 7
  • 25

1 Answers1

1

Make sure you have added encryption type in your form

  enctype="multipart/form-data"
Mudit Gulgulia
  • 1,131
  • 7
  • 21