且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP更改文件的扩展名?

更新时间:2022-06-22 22:37:50

在现代操作系统中,文件名可能很好地包含文件扩展名之前的句点,例如:

In modern operating systems, filenames very well might contain periods long before the file extension, for instance:

my.file.name.jpg

PHP提供了一种无需扩展名即可查找文件名的方法,只需添加新扩展名即可:

PHP provides a way to find the filename without the extension that takes this into account, then just add the new extension:

function replace_extension($filename, $new_extension) {
    $info = pathinfo($filename);
    return $info['filename'] . '.' . $new_extension;
}