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

- 4,351
- 24
- 32
-
1If 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
You could try this
$file = Input::file('upfile')->getClientOriginalName();
$filename = pathinfo($file, PATHINFO_FILENAME);
$extension = pathinfo($file, PATHINFO_EXTENSION);
echo $filename . ' ' . $extension; // 'qwe jpg'

- 14,930
- 5
- 44
- 45
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());
This one is pretty clean:
$fileName = pathinfo($fullFileName)['filename'];
Equivalent to:
$fileName = pathinfo($fullFileName, PATHINFO_FILENAME);

- 37,872
- 26
- 173
- 191
here you need to call php PATHINFO_FILENAME
$file = $request->file('fileupload')->getClientOriginalName();
$fileName = pathinfo($file,PATHINFO_FILENAME);
dd($fileName);

- 611
- 2
- 11
- 26
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'
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);
}

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

- 126
- 2
- 4
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];

- 579
- 8
- 14
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);

- 5,381
- 6
- 47
- 68
preg_replace('/\..+$/', '', 'qwe.jpg')
or
explode('.', 'qwe.jpg')[0]

- 16,404
- 12
- 44
- 69

- 21
- 2
-
2Just a small notice, what about .tar.gz files with your solution? ;) – Daniel Steiner Apr 15 '16 at 13:02
-
the best for cleaness, but works in 90% of cases, it depends on the situation if it could be a correct option – Luca C. Aug 02 '16 at 14:04
you can use this
Input::file('upfile')->getClientOriginalExtension()

- 7
- 3
-
1Are 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