0

I would like to know how to parse the text of a file name so it keeps the extension.

so someimage.jpg would become .jpg. This is so I can change the file name but leave the extension.

Thanks.

user377419
  • 4,681
  • 13
  • 42
  • 56
  • 3
    `*yawn*` exact duplicate of zillion similar questions like [How to get the file extension?](http://stackoverflow.com/questions/4841223/how-to-get-the-file-extension) – Your Common Sense Jan 31 '11 at 21:26

1 Answers1

5

Use pathinfo() and pass the PATHINFO_EXTENSION flag to get the file extension. You need to add the dot yourself as the function leaves it out:

$oldname = 'someimage.jpg';
$ext = pathinfo($oldname, PATHINFO_EXTENSION);
rename($oldname, 'thisimage.' . $ext);
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    I assume the downvote must have come from none other than the Colonel himself. Don't worry, I was **expecting** it :) – BoltClock Jan 31 '11 at 21:37
  • @nikic sure, they are. always greedy to snatch some rep points rather than close duplicated question. – Your Common Sense Jan 31 '11 at 22:51
  • 1
    @Col: This is already being discussed on meta and meta is the only place there a decision may be provoked. Downvoting answers is no good, instead convince people on meta that a) rep on duplicate question shall be dropped (the idea is quite popular, actually) and/or b) rep is given for finding duplicates and/or voting to close as duplicate. – NikiC Jan 31 '11 at 22:57