php 在laravel中获取没有扩展名的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33776197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get file name without extension in laravel?
提问by Sumit
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.
我曾经Input::file('upfile')->getClientOriginalName()
检索上传文件的名称,但给出的名称带有扩展名,例如qwe.jpg
.How do I get name without extension like qwe
in laravel.
回答by Maxim Lanin
Laravel uses Symfony UploadedFile
component that will be returned by Input::file()
method.
Laravel 使用UploadedFile
将通过Input::file()
方法返回的Symfony组件。
It hasn't got any method to retrive file name, so you can use php native function pathinfo()
:
它没有任何检索文件名的方法,因此您可以使用php本机函数pathinfo()
:
pathinfo(Input::file('upfile')->getClientOriginalName(), PATHINFO_FILENAME);
回答by Pantelis Peslis
You could try this
你可以试试这个
$file = Input::file('upfile')->getClientOriginalName();
$filename = pathinfo($file, PATHINFO_FILENAME);
$extension = pathinfo($file, PATHINFO_EXTENSION);
echo $filename . ' ' . $extension; // 'qwe jpg'
回答by Sethu
In Laravel 5.2
在 Laravel 5.2 中
Use Request and it's already present in the app file in the aliases array
使用 Request 并且它已经存在于别名数组中的应用程序文件中
To retrieve original file name
检索原始文件名
$request->file('upfile')->getClientOriginalName();
To get file name without extension
获取没有扩展名的文件名
basename($request->file('upfile')->getClientOriginalName(), '.'.$request->file('upfile')->getClientOriginalExtension());
回答by Yevgeniy Afanasyev
This one is pretty clean:
这个很干净:
$fileName = pathinfo($fullFileName)['filename'];
Equivalent to:
相当于:
$fileName = pathinfo($fullFileName, PATHINFO_FILENAME);
回答by Muhammad Kazim
here you need to call php PATHINFO_FILENAME
在这里你需要调用 php PATHINFO_FILENAME
$file = $request->file('fileupload')->getClientOriginalName();
$fileName = pathinfo($file,PATHINFO_FILENAME);
dd($fileName);
回答by TechPotter
Get the file name using getClientOriginalName(); then use the explode function to get the name and the image format as shown below:
使用 getClientOriginalName() 获取文件名;然后使用explode函数获取名称和图片格式如下图:
$image=Input::file('image');
$fullName=$image->getClientOriginalName();
$name=explode('.',$fullName)[0];
$image=Input::file('image');
$fullName=$image->getClientOriginalName();
$name=explode('.',$fullName)[0];
回答by I Made Pustikayasa
I use this code and work in my Laravel 5.2.*
我使用此代码并在我的 Laravel 5.2.* 中工作
$file=$request->file('imagefile');
$imgrealpath= $file->getRealPath();
$nameonly=preg_replace('/\..+$/', '', $file->getClientOriginalName());
$fullname=$nameonly.'.'.$file->getClientOriginalExtension();
回答by silent
preg_replace('/\..+$/', '', 'qwe.jpg')
or
或者
explode('.', 'qwe.jpg')[0]
回答by smartrahat
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);
回答by Mete YILMAZ
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);
}