laravel image upload error

1.1k views Asked by At

i'm using intervention package to upload images with laravel and all things are fine from creating to saving image but the problem in the image column in database it stores this name with path

/private/var/folders/18/0w78rt691m99y_kv8xln4n0c0000gn/T/phpFkP3Gh

this is my code:

if($request->hasFile('image')) {
  $image = $request->file('image');
  $filename = time() . '.' . $image->getClientOriginalExtension();
  $location = public_path('images/' . $filename);
  \Image::make($image)->save($location);
 }

and this is the image name stored in images file 1503847676.jpg

please help me to find, where is the problem?

this is my whole method

public function update(Request $request, $id)
    {

        // Validating fields requests
        $this->validate($request, [

            'title'     =>  'required|min:6',
            'subtitle'  =>  'required|min:6',
            'slug'      =>  'required',
            'body'      =>  'required',
            'image'     =>  'required|mimes:jpeg,png,jpg,gif,svg'
        ]);

        if($request->hasFile('image')) {
           $image = $request->file('image');
           $filename = time() . '.' . $image->getClientOriginalExtension();
           $location = public_path('images/' . $filename);
           \Image::make($image)->save($location);
        }

        //find target post
        $post = Post::find($id);

        //create upadeted data from inputs fields
        $post->title = $request->title;
        $post->subtitle = $request->subtitle;
        $post->image = $filename;
        $post->slug = $request->slug;
        $post->image = $request->image;
        $post->status = $request->status;
        $post->body = $request->body;

        //save the new data to database
        $post->save();
        $post->tags()->sync($request->tags);
        $post->categories()->sync($request->categories);

        return redirect('/admin/post');


    }
3

There are 3 answers

1
Sreejith BS On

You don't need Intervention package for simple image upload w/o any modifications like resize etc. Try this

if( $request->hasFile('image')) {
    $image = $request->file('image');
    $location = public_path(). '/images/';
    $filename = time() . '.' . $image->getClientOriginalExtension();
    $image->move($location, $filename);

}
//find target post
$post = Post::find($id);
$post->image = $filename;

//remaining code
$post->save();
0
dbudimir On

You can try this if you use Intervention image

$image = Input::file('image');
$filename  = time() . '.' . $image->getClientOriginalName();
$path = public_path('images/' . $filename);
Image::make($image->getRealPath())->save($path);
$post->image = 'images/' . $filename;
0
hiDemo Studio On

I had a similar error like yours, I solved it by using the following:

$file = Input::file('image');
$image->move($location, $file->getClientOriginalName());