繁体   English   中英

Laravel 5.5将文件名另存为* .tmp

[英]Laravel 5.5 save filename to database as *.tmp

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

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”已在表单中启用。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM