替换文件名中的点 [英] Replacing dots in a filename

查看:75
本文介绍了替换文件名中的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不丢失文件扩展名的情况下用下划线替换点?

How should I replace dots with underlines without losing the file extension?

$str = $_FILES['files']['name']; //file.name.word.jpg
$ext = end(explode('.', $str));
$filename = explode('.', $str);
//output file_name_word.jpg

ps:必须在上传之前..如果用户上传带点的文件,则必须重命名该文件并将其插入db

ps: it needs to be before upload.. if the user uploads a file with dots it must to be renamed and inserted on db

推荐答案

使用 pathinfo() 进行提取文件名和 str_replace() 删除其中的所有点.

Use pathinfo() to extract the file name and str_replace() to remove all the dots out of it.

$filename = pathinfo('/path/to/your/file');
echo str_replace('.', '_', $filename['filename']);

这篇关于替换文件名中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆