Get File Extension Using PHP

There are several ways to grab file extension (E.g, 'txt' from 'my.txt') using PHP. You can use anyone from flowing. But I prefer to use pathinfo function, because it is PHP's build in function to grab file path information and others are string functions.



1. Using end>explode Function:
$ext = end(explode('.', $filename));

2. Using substr>strrchr Function:
$ext = substr(strrchr($filename, '.'), 1);

3. Using preg_replace Function:
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);

Finally pathinfo Function (PHP Build in Function to Grab File Path Information):
$ext = pathinfo($filename, PATHINFO_EXTENSION);

5 comments:

  1. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    animal jam | five nights at freddy's | hotmail login

    ReplyDelete
  2. Thanks for the information you brought to us. They are very interesting and new. Look forward to reading more useful and new articles from you!
    wings io

    ReplyDelete
  3. Many thanks for sharing this very diverse opinion post where each expert has no doubt shared his best knowledge on the topic. Have more success in your journey.
    html color

    ReplyDelete

Go to top