get file name without extension in laravel?

前端 未结 13 918
温柔的废话
温柔的废话 2021-02-05 01:14

I have used Input::file(\'upfile\')->getClientOriginalName() to retrieve name of uploaded file but gives name with extension like qwe.jpg.How do I g

13条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 01:46

    In Laravel 7 the Fluent Strings were introduced which allows to do this in an extremely elegant way.

    The basename method will return the trailing name component of the given string:

    use Illuminate\Support\Str;
    
    $string = Str::of('/foo/bar/baz.jpg')->basename();
    
    // 'baz.jpg'
    

    If needed, you may provide an "extension" that will be removed from the trailing component:

    use Illuminate\Support\Str;
    
    $string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
    
    // 'baz'
    

提交回复
热议问题