MilosPusic's avatar

Hello Guys, need help uploading image

Hello to all, im new to laravel, and need some help with uploading images. I created DB and got all set up, but when i upload image, image is saved as temp file and not image file, here is my code

public function add() {

    $file = Request::file('filefield');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
    $entry = new Fileentry();
    $entry->mime = $file->getClientMimeType();
    $entry->original_filename = $file->getClientOriginalName();
    $entry->filename = $file->getFilename().'.'.$extension;
    $entry->img_path = $file->getPath();
    $file->move(base_path() . '/public/images/catalog/');
    $entry->save();

    return redirect('fileentry');
    
}
public function get($filename)
{
    $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
    $file = Storage::disk('local')->get($entry->filename);

    return(new Response($file, 200))
            ->header('Content-Type', $entry->mime);
}

Any idea how to fix that? Thx in Advance

0 likes
1 reply
ohffs's avatar

I think you should pass a new filename to $file->move() as well as the path? Something like $file->move(base_path() . '/public/images/catalog/', md5($file->getClientOriginalName()) . $extension);

Please or to participate in this conversation.