The image failed to upload.on a server laravel
Asked Answered
C

4

6

The image failed to upload.

link https://comedoruniversitariouncp.000webhostapp.com/products/create

The project works in local server, the error appears when i upload to a server

create.blade.php

<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="form-group row">
            <label class="col-form-label col-sm-2">Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="name">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-form-label col-sm-2">Price</label>
            <div class="col-sm-10">
                <input type="number" class="form-control" name="price" step="0.1">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-form-label col-sm-2">Amount</label>
            <div class="col-sm-10">
                <input type="number" class="form-control" name="amount" >
            </div>
        </div>
        <div class="form-group row">
            <label class="col-form-label col-sm-2">Image</label>
            <div class="col-sm-10">
                <input type="file" class="form-control-file" name="image">
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Create</button>
    </form>

ProductController.php

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'price' => 'required',
            'amount' => 'required',
            'image' => 'required|image'
        ]);

        $image = $request->file('image');

        $new_name = rand() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $new_name);

        Product::create([
            'name' => $request->name,
            'price' => $request->price,
            'amount' => $request->amount,
            'image' => $new_name
        ]);

        return redirect()->route('products.index')->with('message', 'Product created successfully');
    }
Cain answered 24/1, 2020 at 4:39 Comment(1)
What is the error>Prong
M
6

As you mention about works on local but not remote. I assumed that the upload_max_filesize is greater than the size your upload file, and both on local and remote are not the same.

Majewski answered 28/5, 2021 at 9:37 Comment(1)
Exactly. This error is returned from Validate function, in my case i resolved changing value of variable upload_max_filesize on php.ini.Miff
E
0

You should try this

Try with adding mimetypes in validation of image.

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'price' => 'required',
            'amount' => 'required',
            'image' => 'required|mimes:jpeg,bmp,png'
        ]);

        $image = $request->file('image');

        $new_name = rand() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $new_name);

        Product::create([
            'name' => $request->name,
            'price' => $request->price,
            'amount' => $request->amount,
            'image' => $new_name
        ]);

        return redirect()->route('products.index')->with('message', 'Product created successfully');
    }
Evite answered 24/1, 2020 at 4:53 Comment(0)
H
0

You may use The Storage Facade as a convenient way to interact with your local filesystems.

use Illuminate\Support\Facades\Storage;
//...
$new_name = rand() . '.' . $image->getClientOriginalExtension();
Storage::disk('public')->putFileAs('images', request->file('image'), $new_name);
//...

Docs

Hectoliter answered 24/1, 2020 at 4:57 Comment(0)
D
0

You Should Try code this,

you may change a part code :

$image->move(public_path('images'), $new_name);

to be code :

$image->move(public_path('images'.$new_name));

this is code, 100% work to me.

Diphosgene answered 28/2, 2021 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.