【问题标题】:Laravel 5.5 save filename to database as *.tmpLaravel 5.5 将文件名保存到数据库为 *.tmp
【发布时间】:2018-05-20 09:03:51
【问题描述】:

我想用链接的图片保存我的帖子/ 我的模型:

class Performer extends Model
{
    protected $fillable = ['title','slug','logoimage','description','address','toplace','exp','workers','published','created_by','modified_by'];

    public function categories() {
        return $this->morphToMany('App\Category', 'categoryable');
    }

    public function SetSlugAttribute($value)
  {
    $this->attributes['slug'] = Str::slug(mb_substr($this->title, 0, 40) . "-". \Carbon\Carbon::now()->format('dmyHi'), '-');
  }
}

我的控制器:

public function store(Request $request) {

       // dd($request);
        $performer = Performer::create($request->all());

        if ($request->input('categories')){
            $performer->categories()->attach($request->input('categories'));
        }

        if ($request->hasfile('logoimage')){
          $image = $request->file('logoimage');
          $filename = time().'.'.$image->getClientOriginalExtension();
          $location = public_path('images/uploads/logo/'.$filename);
          Image::make($image)->resize(100, 100)->save($location);
          // dd($filename); - return normal filename, 857857857.jpg as example
          $performer->logoimage= $filename;

        }


        return redirect()->route('admin.performer.index');
    }

查看:

<input type="file" class="form-control" name="logoimage">

enctype="multipart/form-data" 在表单中启用。

结果: 图像通常保存到文件夹 public\images\uploads\logo,名称为 *.jpg 到数据库(logoimage 列)保存为 C:\xampp\tmp\php915C.tmp。 为什么? 如何解决?

【问题讨论】:

    标签: php laravel laravel-5.5 intervention


    【解决方案1】:

    问题解决了。我添加了 $performer->save();到我的控制器 - 它对我来说很好。

    【讨论】:

      【解决方案2】:

      不要使用 GetClientOriginalExtension。请改用 GetClientOriginalName 以获得最佳实践。您提出的方法有效,但您没有“必须”添加扩展名。您将获得 .tmp 的原因是因为您没有获得原始文件名。但就像我提到的,您也可以在代码末尾使用扩展。 希望这对遇到此问题的人有所帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多