簡體   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