Change file extension

前端 未结 5 1642
遇见更好的自我
遇见更好的自我 2021-01-12 06:46

How do I change a files file-extension name in PHP?

For example: $filename=\'234230923_picture.bmp\' and I want the extension to change to jpg

5条回答
  •  一个人的身影
    2021-01-12 07:20

    Not using regex (like the basename example), but allowing multiple extension possibilities (like the regex example):

    $newname = str_replace(array(".bmp", ".gif"), ".jpg", $filename);
    rename($filename, $newname);
    

    Of course any simple replace operation, while less expensive then regex, will also replace a .bmp in the middle of the filename.

    As mentioned, this isn't going to change the format of a image file. To do that you would need to use a graphics library.

提交回复
热议问题