55

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Sumit
  • 1,235
  • 4
  • 17
  • 29
  • Does this answer your question? [How do I get (extract) a file extension in PHP?](https://stackoverflow.com/questions/173868/how-do-i-get-extract-a-file-extension-in-php) – miken32 Mar 29 '20 at 23:43

12 Answers12

90

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);
Maxim Lanin
  • 4,351
  • 24
  • 32
  • 1
    If there is $request, instead of Input model, the code should as '$filename = pathinfo($request->file('import_file')->getClientOriginalName(), PATHINFO_FILENAME);' – Nuwan Withanage May 03 '19 at 04:27
60

You could try this

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

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

echo $filename . ' ' . $extension; // 'qwe jpg'
Pantelis Peslis
  • 14,930
  • 5
  • 44
  • 45
18

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());
kamal pal
  • 4,187
  • 5
  • 25
  • 40
Sethu
  • 195
  • 1
  • 8
14

This one is pretty clean:

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

Equivalent to:

  $fileName = pathinfo($fullFileName, PATHINFO_FILENAME);

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

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
11

here you need to call php PATHINFO_FILENAME

$file = $request->file('fileupload')->getClientOriginalName();
$fileName = pathinfo($file,PATHINFO_FILENAME);
dd($fileName);
Muhammad Kazim
  • 611
  • 2
  • 11
  • 26
8

In Laravel 7 the Fluent Strings were introduced which allows to do this in an extremely elegant way.

The basename method will return the trailing name component of the given string:

use Illuminate\Support\Str;

$string = Str::of('/foo/bar/baz.jpg')->basename();

// 'baz.jpg'

If needed, you may provide an "extension" that will be removed from the trailing component:

use Illuminate\Support\Str;

$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');

// 'baz'
Dharman
  • 30,962
  • 25
  • 85
  • 135
MrEduar
  • 1,784
  • 10
  • 22
4

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);
    }
  • 1
    If my file name is "natural.picture.png", then is it work? – Yagnesh bhalala Feb 05 '19 at 11:11
  • I tested your succession. It works fine. The code generating new name and saving the extension correctly. I'm using Laravel 5.7 by the way. – Mete YILMAZ Feb 10 '19 at 20:49
  • i know this old tread, but that will failed with `natural.picture.png` the `name` will be return natural, and in the end this saving the file using `time()` – Rio A.P Sep 17 '20 at 06:57
3

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

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

TechPotter
  • 579
  • 8
  • 14
2

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);
smartrahat
  • 5,381
  • 6
  • 47
  • 68
1
preg_replace('/\..+$/', '', 'qwe.jpg')

or

explode('.', 'qwe.jpg')[0]
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
silent
  • 21
  • 2
-5

you can use this

Input::file('upfile')->getClientOriginalExtension()
  • 1
    Are you sure about this? The OP asked for the file name without the extension, calling `getClientOriginalExtension` returns the extension only.... – Nico Haase Sep 10 '18 at 10:11
  • this is how you can get it, missed 2nd line earlier $ext = Input::file('upfile')->getClientOriginalExtension(); echo basename($path, '.'.$ext); – Mehfooz ul haq Sep 10 '18 at 10:23
  • Please edit your answer such that it contains all relevant information, this includes both the code and a short explanation about the code – Nico Haase Sep 10 '18 at 11:44
  • this is how you can get it, missed 2nd line earlier `$ext = Input::file('upfile')->getClientOriginalExtension(); echo basename($path, '.'.$ext);` – Mehfooz ul haq Sep 10 '18 at 11:58
  • Still, please edit your answer and add all relevant information there, not to the comment section – Nico Haase Sep 10 '18 at 12:04
  • `getClientOriginalExtension()` will only return the file extension not the filename. – Waiyl Karim Mar 18 '20 at 20:32