按日期创建文件名Laravel

时间:2018-03-28 02:57:36

标签: laravel

问题是它没有按日期保存文件名。我在dd($date);检查它是否正确。但是当我按日期存储到文件名时。它不起作用。

MaterialsController @存储

  $date = now()->format('Y-m-d-H-i-s');
  $destinationPath = config('app.filesDestinationPath').'/'.$date.'/';
foreach ($files as $file) {
  $fileName = $file->getClientOriginalName();
  $uploaded = Storage::put($destinationPath.$fileName.'.'.$file->getClientOriginalExtension(),file_get_contents($file->getRealPath()));
}
if($uploaded){
   $date = now()->format('Y-m-d-H-i-s');
   Material::create(['description' => $request->input('description'),
            'title' => $request->input('title'),
            'filename' => $date]);
}

MaterialsController @展示

public function show(Material $material)
 {
    $material = Material::find($material->id);
    $directory = config('app.filesDestinationPath').'/'.$material->filename.'/';
    $files = Storage::files($directory);
    return view('materials.show', ['material'=>$material])->with(array('files' => $files));
 }

1 个答案:

答案 0 :(得分:0)

尝试使用Storage :: makeDirectory(),所以你的代码会是这样的(顺便说一句,我建议Y-m-d,所以它不会制作很多文件夹)

$date = date('Y-m-d');
Storage::makeDirectory($date);