Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pecelchikin's avatar

uploaded image stored as .tmp file in database laravel 9

this is my first time using laravel. i want to save the image that i have uploaded and display the filename of the image in database table and show the thumbnail of my uploaded image in my web page. however, when I try to upload the image, it is saved in the database but it only shows the location where the uploaded file is stored as .tmp in xampp.

but the image that I uploaded is with the file name that matches what I want in the folder 'public/uploads/books/' but why does it become C:\xampp\tmp\php5373.tmp file in the database? help me pls

here is my controller

public function store (BukuFormRequest $request){

        $validatedData = $request->validated();
        $buku = new Buku;
        $buku->id_buku= $validatedData['id_buku'];

        if($request->hasFile('cover_buku')){
            $file = $request->file('cover_buku');
            $ext = $file->getClientOriginalExtension();
            $filename = time().'.'.$ext; 
            $file->move('uploads/buku/', $filename); 
            $buku->cover_buku = $filename;
            }

        $buku->cover_buku       = $validatedData['cover_buku'];
        $buku->judul_buku       = $validatedData['judul_buku'];
        $buku->nama_penulis     = $validatedData['nama_penulis'];
        $buku->tgl_terbit       = $validatedData['tgl_terbit'];
        $buku->halaman          = $validatedData['halaman'];
        $buku->ukuran           = $validatedData['ukuran'];
        $buku->isbn             = $validatedData['isbn'];
        $buku->keterangan_buku  = $validatedData['keterangan_buku'];
        $buku->slug             = Str::slug($validatedData['slug']);
        $buku->status_buku      = $request->status_buku == true ? '1' : '0';
        
        $buku->save();

        return redirect('admin/buku') -> with('message', 'Data Buku Berhasil Ditambahkan!');
    }

here is the rules

public function rules()
    {
        'cover_buku' => [
          'nullable',
          'image',
          'mimes:jpg,jpeg,png',
          'max:5120'
         ]
0 likes
7 replies
newbie360's avatar
Level 24

the cover overwrited by this line

$buku->cover_buku       = $validatedData['cover_buku'];
1 like
newbie360's avatar

@pecelchikin for me, i will do this instead

in Buku model add this

    protected $casts = [
        'id_buku' => 'integer',
        'status_buku' => 'boolean',
    ];

make sure in model $fillable has all the needed fields

public function store(BukuFormRequest $request)
{
        $filename = null;

        if($request->hasFile('cover_buku')){
            $file = $request->file('cover_buku');
            $filename = time().'.'.$file->extension();
        }

        Buku::create(
            $request->safe()->merge([
                'cover_buku' => $filename,

                // be careful slug should be unique, otherwise need some logic to handle this
                // and also you can move this to Oberver or Mutator, anyway...
                'slug' => Str::slug($request->validated()['slug']),
            ])
        );

        // Ensure database layer no error then move file
        if ($filename) {
            $file->move('uploads/buku/', $filename);
        }

        return redirect('admin/buku') -> with('message', 'Data Buku Berhasil Ditambahkan!');
    }
}
1 like
pecelchikin's avatar

@newbie360 yuppp i'll make sure again about model, $fillable in my project. and thank you for your suggestion!!

MohamedTammam's avatar

This part

if($request->hasFile('cover_buku')){
            $file = $request->file('cover_buku');
            $ext = $file->getClientOriginalExtension();
            $filename = time().'.'.$ext; 
            $file->move('uploads/buku/', $filename); 
            $buku->cover_buku = $filename;
}

$buku->cover_buku       = $validatedData['cover_buku'];

Can be shorten and fixed by this

$buku->cover_buku =  $request->hasFile('cover_buku') ?  $request->cover_buku->store('public', 'uploades/buku') : null;

I assume that uploads directory in the public storage folder which is storage/app/public

1 like
pecelchikin's avatar

@MohamedTammam thank you for your suggestion! but my uploads/buku directory not in the storage folder.. when i first faced that error, i also thought that the public folder might be in the storage/app directory but when i checked the folder, not in that storage/app

1 like

Please or to participate in this conversation.