如何使用 PHP 更改文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/193794/
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
How can I change a file's extension using PHP?
提问by PHLAK
How can I change a file's extension using PHP?
如何使用 PHP 更改文件的扩展名?
Ex: photo.jpg to photo.exe
例如:photo.jpg 到 photo.exe
回答by Tony Maro
In modern operating systems, filenames very well might contain periods long before the file extension, for instance:
在现代操作系统中,文件名很可能在文件扩展名之前很久就包含句点,例如:
my.file.name.jpg
PHP provides a way to find the filename without the extension that takes this into account, then just add the new extension:
PHP 提供了一种方法来查找没有考虑到这一点的扩展名的文件名,然后只需添加新的扩展名:
function replace_extension($filename, $new_extension) {
$info = pathinfo($filename);
return $info['filename'] . '.' . $new_extension;
}
回答by Matt
substr_replace($file , 'png', strrpos($file , '.') +1)
Will change any extension to what you want. Replace png with what ever your desired extension would be.
将任何扩展更改为您想要的。用您想要的扩展名替换 png。
回答by Alex
Replace extension, keep path information
替换扩展名,保留路径信息
function replace_extension($filename, $new_extension) {
$info = pathinfo($filename);
return ($info['dirname'] ? $info['dirname'] . DIRECTORY_SEPARATOR : '')
. $info['filename']
. '.'
. $new_extension;
}
回答by Jeremy Ruten
Once you have the filename in a string, first use regex to replace the extension with an extension of your choice. Here's a small function that'll do that:
在字符串中包含文件名后,首先使用正则表达式将扩展名替换为您选择的扩展名。这是一个可以做到这一点的小函数:
function replace_extension($filename, $new_extension) {
return preg_replace('/\..+$/', '.' . $new_extension, $filename);
}
Then use the rename()function to rename the file with the new filename.
然后使用rename()函数用新文件名重命名文件。
回答by niksmac
Just replace it with regexp:
只需用正则表达式替换它:
$filename = preg_replace('"\.bmp$"', '.jpg', $filename);
You can also extend this code to remove other image extensions, not just bmp:
您还可以扩展此代码以删除其他图像扩展名,而不仅仅是 bmp:
$filename = preg_replace('"\.(bmp|gif)$"', '.jpg', $filename);
回答by Enyby
Better way:
更好的方法:
substr($filename, 0, -strlen(pathinfo($filename, PATHINFO_EXTENSION))).$new_extension
Changes made only on extension part. Leaves other info unchanged.
仅对扩展部分进行了更改。保持其他信息不变。
It's safe.
它是安全的。
回答by mgutt
You could use basename():
您可以使用basename():
$oldname = 'path/photo.jpg';
$newname = (dirname($oldname) ? dirname($oldname) . DIRECTORY_SEPARATOR : '') . basename($oldname, 'jpg') . 'exe';
Or for all extensions:
或者对于所有扩展:
$newname = (dirname($oldname) ? dirname($oldname) . DIRECTORY_SEPARATOR : '') . basename($oldname, pathinfo($path, PATHINFO_EXTENSION)) . 'exe';
Finally use rename():
最后使用rename():
rename($oldname, $newname);
回答by chadi
For regex fans, modified version of Thanh Trung's 'preg_replace' solution that will always contain the new extension (so that if you write a file conversion program, you won't accidentally overwrite the source file with the result) would be:
对于正则表达式粉丝,Thanh Trung 的“preg_replace”解决方案的修改版本将始终包含新扩展名(这样如果您编写文件转换程序,您就不会意外地用结果覆盖源文件)将是:
preg_replace('/\.[^.]+$/', '.', $file) . $extension
回答by Moradnejad
Many good answers have been suggested. I thought it would be helpful to evaluate and compare their performance. Here are the results:
已经提出了许多好的答案。我认为评估和比较它们的性能会有所帮助。结果如下:
- answer by Tony Maro (
pathinfo) took 0.000031040740966797 seconds. Note: It has the drawback for not including full path. - answer by Matt (
substr_replace) took 0.000010013580322266 seconds. - answer by Jeremy Ruten (
preg_replace) took 0.00070095062255859 seconds.
- Tony Maro (
pathinfo) 的回答耗时 0.000031040740966797 秒。注意:它的缺点是不包括完整路径。 - Matt (
substr_replace) 的回答用了 0.000010013580322266 秒。 - Jeremy Ruten (
preg_replace) 的回答耗时 0.00070095062255859 秒。
Therefore, I would suggest substr_replace, since it's simpler and faster than others.
因此,我建议substr_replace,因为它比其他的更简单、更快。
Just as a note, There is the following solution too which took 0.000014066696166992 seconds. Still couldn't beat substr_replace:
请注意,以下解决方案也花费了 0.000014066696166992 秒。还是打不过substr_replace:
$parts = explode('.', $inpath);
$parts[count( $parts ) - 1] = 'exe';
$outpath = implode('.', $parts);

