Reputation: 2139
I created a form with file
and uploads the file and stores the data in the database very well. The problem is, I need to store the modified file name in the database but the Laravel stores the temporary name in the database. This is the code
public function store(Request $request)
{
$image = $request->file('file');
$imageName = time().rand(1,100).$image->getClientOriginalName();
$image->move(public_path('uploads'),$imageName);
$request['file'] = $imageName;
//$request->file = $imageName;
$im = new Image($request->all());
$this->user->images()->save($im);
}
I tried to modify the file
manually but it didn't work. This the dd
of $request
But still the temporary file name is inserted in to database.
This is the table and file
column must have the name of the file
As you see the file name I provided is not in the file
column, the temporary is in there
Upvotes: 1
Views: 2514
Reputation: 1020
as @Haritsinh Gohil described
As you have printed $request array on screen, the uploaded file name has changed as per your desired name,
but problem arises when you use $request->all() method, see below the all() method in Illuminate/Http/Concerns/InteractsWithInput.php
However, you can keep the input with the name file and make
$image = $request->file('file');
$imageName = time().rand(1,100).$image->getClientOriginalName();
$image->move(public_path('uploads'),$imageName);
$data = $request->all();
$data['file'] = $imageName;
$im = new Image($data);
$this->user->images()->save($im)
Upvotes: 0
Reputation: 133
You have to change name from:
<input name="file" type="file"/>
to:
<input name="upload_file" type="file"/>
Upvotes: 1
Reputation: 6272
As you have printed $request
array on screen, the uploaded file name has changed as per your desired name,
but problem arises when you use $request->all()
method, see below the all()
method in Illuminate/Http/Concerns/InteractsWithInput.php
public function all($keys = null)
{
$input = array_replace_recursive($this->input(), $this->allFiles());
if (! $keys) {
return $input;
}
$results = [];
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
Arr::set($results, $key, Arr::get($input, $key));
}
return $results;
}
The above method replaces the normal input keys with file input keys if both have same name, means if you have $request['image']
and $request->file('image')
then after calling $request->all()
your $request['image']
is bound to replaced by $request->file('image')
.
So what to do if you don't want to replace it automatically like here you want to get newly uploaded file name in $request['file']
instead of tmp\php23sf.tmp,
one workaround is to use different name in file input and db field name, lets take your example:
file
for storing uploaded filename so use name userfile
or any other name in file input as <input type="file" name="userfile">
see below:
public function store(Request $request)
{
$image = $request->file('userfile');
$imageName = time().rand(1,100).$image->getClientOriginalName();
$image->move(public_path('uploads'),$imageName);
$request['file'] = $imageName;
$im = new Image($request->all());
$this->user->images()->save($im);
}
It will work definitely, correct me if i am wrong or ask me anything if you want further info, thanks.
Upvotes: 2
Reputation: 9389
After looking at the output you have provided, I think here is your mistake.
$imageName = time().rand(1,100).$image->getClientOriginalName();
You have to add Original Extension instead of Original Name like this,
$imageName = time().rand(1,100).$image->getClientOriginalExtension();
I hope you understand.
Upvotes: -1