Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

file

php

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

Ex: photo.jpg to photo.exe

like image 310
PHLAK Avatar asked Oct 11 '08 07:10

PHLAK


1 Answers

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:

function replace_extension($filename, $new_extension) {     $info = pathinfo($filename);     return $info['filename'] . '.' . $new_extension; } 
like image 72
Tony Maro Avatar answered Sep 19 '22 18:09

Tony Maro