M a m a D
M a m a D

Reputation: 2139

Laravel 5.6 - Cannot change the uploaded file name in $request, temporary name is inserted in the database

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

enter image description here

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

enter image description here

As you see the file name I provided is not in the file column, the temporary is in there

Upvotes: 1

Views: 2514

Answers (4)

Amr Abdalrahman Ahmed
Amr Abdalrahman Ahmed

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

Hitesh Khunt
Hitesh Khunt

Reputation: 133

You have to change name from:

<input name="file" type="file"/>

to:

<input name="upload_file" type="file"/>

Upvotes: 1

Haritsinh Gohil
Haritsinh Gohil

Reputation: 6272

Reason why its happen:

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,

Solution:

one workaround is to use different name in file input and db field name, lets take your example:

  1. You have database table field file for storing uploaded filename so use name userfile or any other name in file input as <input type="file" name="userfile">
  2. Then after it in your controller use same code as you have used with different name:

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

Sagar Gautam
Sagar Gautam

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

Related Questions