shariff's avatar
Level 51

Image not uploading

public function store(Request $request) { $this->validate($request,[ 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048' ]);

    $image = $request->file('image');
    $name = rand().'.'.$image->getClientOriginalExtension();

    if(file_exists(public_path("images2")))
    {
        $image->move(public_path("images2"),$name);
    }
    else
    {
        $image->move(public_path(mkdir("images2")),$name);
     
    }
}

In the above code if directory exists then the image is moving to the path. Else block code not working only directory is created but image is not moving.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

This

else
    {
        $image->move(public_path(mkdir("images2")),$name);
     
    }

expects the path to be returned from the mkdir command. it probably returns true or false.

alter the logic

    $image = $request->file('image');
    $name = rand().'.'.$image->getClientOriginalExtension();

    if(! file_exists(public_path("images2")))
    {
        public_path(mkdir("images2");
    }
    
    $image->move(public_path("images2"),$name);

Please or to participate in this conversation.