【问题标题】:Laravel 5.6 - Cannot change the uploaded file name in $request, temporary name is inserted in the databaseLaravel 5.6 - 无法更改 $request 中上传的文件名,临时名称插入数据库
【发布时间】:2019-02-02 16:16:01
【问题描述】:

我用file创建了一个表单并上传文件并将数据很好地存储在数据库中。问题是,我需要将修改后的文件名存储在数据库中,但 Laravel 将临时名称存储在数据库中。这是代码

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);
}

我尝试手动修改file,但没有成功。这是$requestdd

但临时文件名仍然插入到数据库中。


这是表格,file 列必须有文件名

如您所见,我提供的文件名不在file 列中,临时文件在其中

【问题讨论】:

  • 上传文件夹中的文件名是否正确?
  • @SagarGautam 是的
  • 添加新的图像名称以请求后,只需像这样转储请求数组,dd($request->all()) 并添加输出
  • 我这样做并添加到图像中
  • 我已经添加了我的答案,看看

标签: laravel laravel-5 laravel-request


【解决方案1】:

查看您提供的输出后,我认为这是您的错误。

$imageName = time().rand(1,100).$image->getClientOriginalName();

您必须像这样添加原始扩展名而不是原始名称,

$imageName = time().rand(1,100).$image->getClientOriginalExtension();

希望你能理解。

【讨论】:

  • 你没有得到问题。 $imageName 是什么并不重要,例如将其设置为 1.jpg。我无法将其插入数据库。类似于C:\xampp\tmp\phpAC28.tmp 的临时名称被插入到表中
  • @Drupalist 请添加您的数据库表结构,虽然奇怪的问题
  • 我添加了表格
【解决方案2】:

发生的原因:

当您在屏幕上打印$request 数组时,上传的文件名已更改为您想要的名称,

但是当你使用$request->all()方法时会出现问题,见下面Illuminate/Http/Concerns/InteractsWithInput.php中的all()方法

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;
    }

上述方法如果两个名称相同,则将普通输入键替换为文件输入键,这意味着如果您有$request['image']$request->file('image'),那么在调用$request->all() 之后,您的$request['image'] 是绑定为$request->file('image')

如果你不想像这里一样自动替换它怎么办你想在$request['file']而不是tmp\php23sf.tmp中获取新上传的文件名,

解决办法:

一种解决方法是在文件输入和数据库字段名称中使用不同的名称,让我们举个例子:

  1. 您有数据库表字段file 用于存储上传的文件名,因此请在文件输入中使用名称userfile 或任何其他名称作为<input type="file" name="userfile">
  2. 然后在您的控制器中使用与您使用不同名称相同的代码:

见下文:

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);
}

它肯定会起作用,如果我错了,请纠正我,或者如果您需要更多信息,请询问我,谢谢。

【讨论】:

    【解决方案3】:

    您必须更改名称:

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

    到:

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

    【讨论】:

      【解决方案4】:

      @Haritsinh Gohil 所述

      当您在屏幕上打印 $request 数组时,上传的文件名 已根据您想要的名称进行更改,

      但是当你使用 $request->all() 方法时会出现问题,见下文 Illuminate/Http/Concerns/InteractsWithInput.php 中的 all() 方法

      但是,您可以将输入与名称文件一起保留并制作

          $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)
      

      【讨论】:

        猜你喜欢
        • 2018-02-14
        • 2017-06-12
        • 2014-03-06
        • 2018-10-19
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        相关资源
        最近更新 更多