Ad

Laravel, Image Not Uploaded

- 1 answer

I'm trying to implement uploading images to the CRUD controller. Everything works separately for me. But as soon as I implement the same code into the controller I still get a message that the file must be an image and have a valid extension

  • The image must be an image. The image must be a file of type: jpeg, png, jpg, gif, svg.

The code is as follows

$product->update($request->all());


    $request->validate([

        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

    ]);

    $imageName = time().'.'.$request->image->extension();
    $request->image->move(public_path('images'), $imageName);

    return redirect()->route('product.index');
    return back()
        ->with('success','You have successfully upload image..');

blade file

@extends('base')

@section('title', 'Vložení produktu') @section('description', 'Editor pro 
vytvoření nového produktu.')

@section('content')
   <div class="form-group">
    <label for="title">Název</label>
    <input type="text" name="title" id="title" class="form-control" value="{{ old('title') }}" required minlength="5" maxlength="80" />
</div>

<div class="form-group">
    <label for="url">URL</label>
    <input type="text" name="url" id="url" class="form-control" value="{{ old('url') }}" required minlength="5" maxlength="80" />
</div>

<div class="form-group">
    <label for="url">URL prodejce</label>
    <input type="text" name="seller_url" id="seller_url" class="form-control" value="{{ old('seller_url') }}" required minlength="5" maxlength="80" />
</div>

<div class="row">
    <div class="form-group col-md-6">
        <label for="url">Cena</label>
        <input type="text" name="price" id="price" class="form-control" value="{{ old('price') }}" />
    </div>
    <div class="form-group col-md-6">
        <label for="url">Stará cena</label>
        <input type="text" name="old_price" id="old_price" class="form-control" value="{{ old('old_price') }}" />
    </div>
</div>



<div class="form-group">
    <label for="url">Obrázky</label>
    <input type="file" name="image" id="image" class="form-control-file"/>
</div>

<button type="submit" class="btn btn-primary">Vytvořit produkt</button>
</form>
@endsection
Ad

Answer

Try this one, maybe helpful for you

$dirpath = public_path('images');
$name = $request['image']->getClientOriginalExtension();
$filename = time().'_'.$name;
$file=$request['image']->move($dirpath, $filename);
Ad
source: stackoverflow.com
Ad