Home » Php » php – get file name without extension in laravel?

php – get file name without extension in laravel?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

You could try this

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

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

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

Answer:

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

Answer:

This one is pretty clean:

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

Equivalent to:

  $fileName = pathinfo($fullFileName, PATHINFO_FILENAME);

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

Answer:

here you need to call php PATHINFO_FILENAME

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

Answer:

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

Answer:

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

Answer:

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

or

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

Answer:

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

Answer:

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

Answer:

you can use this

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

Answer:

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