How can I change a file\'s extension using PHP?
Ex: photo.jpg to photo.exe
Many good answers have been suggested. I thought it would be helpful to evaluate and compare their performance. Here are the results:
pathinfo) took 0.000031040740966797 seconds. Note: It has the drawback for not including full path.substr_replace) took 0.000010013580322266 seconds.preg_replace) took 0.00070095062255859 seconds.Therefore, I would suggest substr_replace, since it's simpler and faster than others.
Just as a note, There is the following solution too which took 0.000014066696166992 seconds. Still couldn't beat substr_replace:
$parts = explode('.', $inpath);
$parts[count( $parts ) - 1] = 'exe';
$outpath = implode('.', $parts);