get file name without extension in laravel?

前端 未结 13 919
温柔的废话
温柔的废话 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'
    
    0 讨论(0)
  • 2021-02-05 01:49

    here you need to call php PATHINFO_FILENAME

    $file = $request->file('fileupload')->getClientOriginalName();
    $fileName = pathinfo($file,PATHINFO_FILENAME);
    dd($fileName);
    
    0 讨论(0)
  • 2021-02-05 01:52

    I use this code and work in my Laravel 5.2.*

    $file=$request->file('imagefile');
    $imgrealpath= $file->getRealPath(); 
    $nameonly=preg_replace('/\..+$/', '', $file->getClientOriginalName());
    $fullname=$nameonly.'.'.$file->getClientOriginalExtension();
    
    0 讨论(0)
  • 2021-02-05 01:53

    You could try this

    $file = Input::file('upfile')->getClientOriginalName();
    
    $filename = pathinfo($file, PATHINFO_FILENAME);
    $extension = pathinfo($file, PATHINFO_EXTENSION);
    
    echo $filename . ' ' . $extension; // 'qwe jpg'
    
    0 讨论(0)
  • 2021-02-05 01:55

    Try this:

        $fullName = Input::file('image')->getClientOrginalName();
        $extension = Input::file('image')->getClientOrginalExtension();
        $onlyName = explode('.'.$extension,$fullName);
    

    Or This:

        $fullName = Input::file('image')->getClientOrginalName();
        $extension = Input::file('image')->getClientOrginalExtension();
    
        $fullNameLenght = strlen($fullName);
        $extensionLenght = strlen($extension);
        $nameLength = $fullNameLenght - ($extensionLength + 1);
        $onlyName = strpos($fullName, 0, $nameLength);
    
    0 讨论(0)
  • 2021-02-05 01:56

    you can use this

    Input::file('upfile')->getClientOriginalExtension()
    
    0 讨论(0)
提交回复
热议问题