get file name without extension in laravel?

2019-04-03 23:15发布

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

12条回答
Summer. ? 凉城
2楼-- · 2019-04-03 23:34

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];

查看更多
贼婆χ
3楼-- · 2019-04-03 23:35

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);
查看更多
Rolldiameter
4楼-- · 2019-04-03 23:46

In Laravel 5.2

Use Request and it's already present in the app file in the aliases array

To retrieve original file name

$request->file('upfile')->getClientOriginalName();

To get file name without extension

basename($request->file('upfile')->getClientOriginalName(), '.'.$request->file('upfile')->getClientOriginalExtension());
查看更多
Summer. ? 凉城
5楼-- · 2019-04-03 23:51

here you need to call php PATHINFO_FILENAME

$file = $request->file('fileupload')->getClientOriginalName();
$fileName = pathinfo($file,PATHINFO_FILENAME);
dd($fileName);
查看更多
来,给爷笑一个
6楼-- · 2019-04-03 23:51
preg_replace('/\..+$/', '', 'qwe.jpg')

or

explode('.', 'qwe.jpg')[0]
查看更多
男人必须洒脱
7楼-- · 2019-04-03 23:52

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

查看更多
登录 后发表回答