MoFish's avatar

Upload keeps storing tmp filename in db

Hi,

I'm trying to upload a file to disk and store the image filename in the database.

It is uploading to disk OK, but keeps writing xampp\tmp\php3985.tmp to the database field 'image'.

I attempted to use $request->offsetUnset('image'); to try to exclude this from the the request->all and set it myself, but that didn't appear to work.

Any help much appreciated.

Thanks,

// upload
$file = $request->file('image');
$file->move(public_path().'/images/',$file->getClientOriginalName());

// set in db
$result = new CollectionItem;
$request->offsetUnset('image');
$result->create($request->all() + [
    'user_id' => '1',
    'image' => $file->getClientOriginalName()
]);
0 likes
4 replies
Charizard's avatar
Level 5

I would avoid using request->all() and specify each field on its own:

$result->create([
	'field1' => $request->field1,
	'field2' => $request->field2,
	...
	'user_id' => 1,
	'image' => $file->getClientOriginalName()
])
Snapey's avatar

don't use the original filename without also qualifying what the image is

abrada's avatar

In one of the projects, I am using code like this

if ($request->hasFile('avatar')) {
  $file = $request->file('avatar')->store('avatars', 'public');
  //then use the variable $file as you want. $file contains uploaded file name
}
abrada's avatar

like "avatars/jYPfyax9eWMiW2ZHObOkowPfvwOxJDaUhbrMy58M.png"

Please or to participate in this conversation.