1

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

M a m a D
  • 1,938
  • 2
  • 30
  • 61

4 Answers4

2

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.

Haritsinh Gohil
  • 5,818
  • 48
  • 50
1

You have to change name from:

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

to:

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

lupz
  • 3,620
  • 2
  • 27
  • 43
Dpak
  • 133
  • 4
0

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)
Amr Abdalrahman Ahmed
  • 920
  • 1
  • 14
  • 19
-1

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.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • You didn't get the problem. It is not important what the `$imageName` is, set this to `1.jpg` for example. I can't insert this in to the database. The temporary name which is something like `C:\xampp\tmp\phpAC28.tmp` is inserted in the table – M a m a D Aug 28 '18 at 06:12
  • @Drupalist please add your db table structure, strange problem though – Sagar Gautam Aug 28 '18 at 06:21
  • I added the table – M a m a D Aug 28 '18 at 06:27