Search code examples
phplaravellaravel-5php-carbon

Carbon laravel adding to file name


I have file upload in my controller and need to add carbon to end of my file so I can insert it into database as unique. I'm doing it like this

        $upload->title = Auth::User()->id;
        $current = Carbon::now();
        $file = $request->file('file');
        $file->move(storage_path(). '/', $file->getClientOriginalName());
        $upload->name = $file->getClientOriginalName().$current;
    }

My question is this, when showing it to user how can I remove it so that the user sees only file original name(with php I can do it but I would like to use "laravel way")


Solution

  • You can't add Carbon::now() to a filename since it has : in some filesystems, so do something like this:

    $current = time(); // Will output something like 1478529571
    

    Or if filenames are different and you just want to add date:

    $current = date('Ymd'); // Will output something like 20161107.
    

    Or, if you want to use Carbon:

    $current = Carbon::now()->format('YmdHs'); // WIll output something like 201611071419