Get file name without extension in laravel?

I used Input::file('upfile')->getClientOriginalName() to get the name of the downloaded file, but gives a name with the extension, for example qwe.jpg . How to get name without extension, e.g. qwe in laravel.

+23
source share
12 answers

Laravel uses the Symfony UploadedFile component, which will be returned by the Input::file() method.

It has no way to get the file name, so you can use the php pathinfo() native function:

 pathinfo(Input::file('upfile')->getClientOriginalName(), PATHINFO_FILENAME); 
+50
source

You can try this

 $file = Input::file('upfile')->getClientOriginalName(); $filename = pathinfo($file, PATHINFO_FILENAME); $extension = pathinfo($file, PATHINFO_EXTENSION); echo $filename . ' ' . $extension; // 'qwe jpg' 
+18
source

In Laravel 5.2

Use the request and it is already present in the application file in an array of aliases

Get the original file name

 $request->file('upfile')->getClientOriginalName(); 

To get the file name without extension

 basename($request->file('upfile')->getClientOriginalName(), '.'.$request->file('upfile')->getClientOriginalExtension()); 
+11
source

This one is pretty clean:

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

Get the file name using getClientOriginalName (); then use the break function to get the name and image format as shown below:

$image=Input::file('image'); $fullName=$image->getClientOriginalName(); $name=explode('.',$fullName)[0];

+3
source

call php PATHINFO_FILENAME here

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

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(); 
+2
source
 preg_replace('/\..+$/', '', 'qwe.jpg') 

or

 explode('.', 'qwe.jpg')[0] 
+1
source

Try the following:

  $fullName = Input::file('image')->getClientOrginalName(); $extension = Input::file('image')->getClientOrginalExtension(); $onlyName = explode('.'.$extension,$fullName); 

Or that:

  $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); 
+1
source

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
source

You can use this

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

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

0
source

All Articles