开发者

Change file extension

开发者 https://www.devze.com 2022-12-11 11:42 出处:网络
How do I cha开发者_如何学编程nge a files file-extension name in PHP? For example: $filename=\'234230923_picture.bmp\' and I want the extension to change to jpg.$newname = basename($filename, \".bmp\"

How do I cha开发者_如何学编程nge a files file-extension name in PHP?

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


$newname = basename($filename, ".bmp").".jpg";
rename($filename, $newname);

Remember that if the file is a bmp file, changing the suffix won't change the format :)


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:

$filename = preg_replace('"\.(bmp|gif)$"', '.jpg', $filename);


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.


rename() the file, substituting the new extension.


You can use this to rename the file http://us2.php.net/rename and this http://us2.php.net/manual/en/function.pathinfo.php to get the basename of the file and other extension info..

0

精彩评论

暂无评论...
验证码 换一张
取 消