Ricardodendulk's avatar

Upload Images Laravel Public Folder Doesn't work

Hi all,

I'm making a Laravel project but i am stuck. I want to upload an image but he does not store it in the public folder. There is no error at all, The only thing is that he doesn't store my image in the public path.

Form create page:

<form method="POST" action="/admin/show" enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="title">Titel</label>
                        <input type="title" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Title" name="title">
                    </div>
                    <div class="form-group">
                        <label for="textarea">Commment</label>
                        <input type="textarea" class="form-control" id="comment" placeholder="Comment" name="body">
                    </div>
                    <div class="form-group">
                        <label for="upload">Upload</label>
                        <input type="file" class="form-control" id="upload" placeholder="upload" name="upload">
                    </div>
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="checkbox" class="form-check-input" name="checked">
                            <!--<input type="text" class="form-check-input" name="checked" value="1">-->
                        </label>
                    </div>

                    <button type="submit" class="btn btn-primary">Submit</button>
                   @include ("layouts.errors")
                </form>

Controller

 public function store(Request $request)
    {

        $this->validate(request(), [
            "title" => "required",
            "body" => "required"
        ]);


        // Make new Project
        $project = new Projects;

        $project->title = $request->title;
        $project->body = $request->body;
        $project->checked = $request->has('checked');



        //Save image
        if($request->hasFile('upload')) {
            $project = $request->file('upload');
            $filename = time() . '.' . $project->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($project)->resize(800, 400)->save($location);

            $project->image = $filename;
        }



        $project->save();

        // Return to a page
        return redirect('admin/index')->with('success', 'Project has been added');


    }

Image Migration

class AddImage extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->string("image")->nullable()->after("checked");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropColumn('image');
        });
    }
}

Thank you in advance.

Cheers

0 likes
1 reply
Snapey's avatar

Check that it passes into the image processing part.

I say this because you misuse $project and I'm surprised it does not throw an error

//up to here $project represents your Projects model (should be singular btw)

        //Save image
        if($request->hasFile('upload')) {
            $project = $request->file('upload');  // but now $project is an instance of uploaded file
            $filename = time() . '.' . $project->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($project)->resize(800, 400)->save($location);  // you pass the whole uploaded file into Image

            $project->image = $filename;  //Now you try and set the filename ON the uploaded image
        }



        $project->save();  // now you try and save the uploaded image instead of the Projects model

Try

        //Save image
        if($request->hasFile('upload')) {
            $upload = $request->file('upload');
            $filename = time() . '.' . $upload->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($upload)->resize(800, 400)->save($location);

            $project->image = $filename;
        }



        $project->save();

Also, check that your application can write to the public folder (check permissions)

Please or to participate in this conversation.