How can I change a file’s extension using PHP?
Ex: photo.jpg to photo.exe
Advertisement
Answer
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; }