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

前端 未结 11 2090
清歌不尽
清歌不尽 2020-12-10 00:43

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

Ex: photo.jpg to photo.exe

11条回答
  •  Happy的楠姐
    2020-12-10 01:09

    Many good answers have been suggested. I thought it would be helpful to evaluate and compare their performance. Here are the results:

    • answer by Tony Maro (pathinfo) took 0.000031040740966797 seconds. Note: It has the drawback for not including full path.
    • answer by Matt (substr_replace) took 0.000010013580322266 seconds.
    • answer by Jeremy Ruten (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);
    

提交回复
热议问题