How can I change a file's extension using PHP?

前端 未结 11 2081
清歌不尽
清歌不尽 2020-12-10 00:43

How can I change a file\'s extension using PHP?

Ex: photo.jpg to photo.exe

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 01:28

    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.

提交回复
热议问题