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条回答
The star\"
2楼-- · 2019-04-03 23:54

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);
    }
查看更多
老娘就宠你
3楼-- · 2019-04-03 23:55

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);
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-03 23:56

You could try this

$file = Input::file('upfile')->getClientOriginalName();

$filename = pathinfo($file, PATHINFO_FILENAME);
$extension = pathinfo($file, PATHINFO_EXTENSION);

echo $filename . ' ' . $extension; // 'qwe jpg'
查看更多
太酷不给撩
5楼-- · 2019-04-03 23:56

you can use this

Input::file('upfile')->getClientOriginalExtension()
查看更多
唯我独甜
6楼-- · 2019-04-03 23:58

This one is pretty clean:

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

查看更多
SAY GOODBYE
7楼-- · 2019-04-03 23:59

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();
查看更多
登录 后发表回答