get file name without extension in laravel?

前端 未结 13 923
温柔的废话
温柔的废话 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:57

    This one is pretty clean:

      $fileName = pathinfo($fullFileName)['filename'];
    

    Equivalent to:

      $fileName = pathinfo($fullFileName, PATHINFO_FILENAME);
    

    https://php.net/manual/en/function.pathinfo.php

    0 讨论(0)
  • 2021-02-05 01:57

    Get the file name using getClientOriginalName(); then use the explode function to get the name and the image format as shown below:

    $image=Input::file('image'); $fullName=$image->getClientOriginalName(); $name=explode('.',$fullName)[0];

    0 讨论(0)
  • 2021-02-05 02:01

    On Laravel 5.4 or Lumen 5.4, this may be a useful resource here.

    0 讨论(0)
  • 2021-02-05 02:04
    preg_replace('/\..+$/', '', 'qwe.jpg')
    

    or

    explode('.', 'qwe.jpg')[0]
    
    0 讨论(0)
  • 2021-02-05 02:06

    You can use this code too.

        if ($request->hasfile('filename')) {
            $image = $request->filename;
            $namewithextension = $image->getClientOriginalName(); //Name with extension 'filename.jpg'
            $name = explode('.', $namewithextension)[0]; // Filename 'filename'
            $extension = $image->getClientOriginalExtension(); //Extension 'jpg'
            $uploadname = time() . '.' . $extension;
            $image->move(public_path() . '/uploads/', $uploadname);
        }
    
    0 讨论(0)
  • 2021-02-05 02:08

    Laravel uses Symfony UploadedFile component that will be returned by Input::file() method.

    It hasn't got any method to retrive file name, so you can use php native function pathinfo():

    pathinfo(Input::file('upfile')->getClientOriginalName(), PATHINFO_FILENAME);
    
    0 讨论(0)
提交回复
热议问题