get file name without extension in laravel?

2019-04-03 23:41发布

问题:

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.

回答1:

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


回答2:

You could try this

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

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

echo $filename . ' ' . $extension; // 'qwe jpg'


回答3:

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());


回答4:

This one is pretty clean:

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



回答5:

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



回答6:

here you need to call php PATHINFO_FILENAME

$file = $request->file('fileupload')->getClientOriginalName();
$fileName = pathinfo($file,PATHINFO_FILENAME);
dd($fileName);


回答7:

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();


回答8:

preg_replace('/\..+$/', '', 'qwe.jpg')

or

explode('.', 'qwe.jpg')[0]


回答9:

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


回答10:

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);
    }


回答11:

you can use this

Input::file('upfile')->getClientOriginalExtension()


回答12:

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