A programming & designing blog!

Sunday, March 17, 2013

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);


  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

  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

  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

  4. The information you share is very useful. It is closely related to my work and has helped me grow. Thank you!
    despacito lyrics

  5. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    NO.1 AQM Services | Application Quality Managment Services | Austere Technologies

  6. This information you provide us should be more visible to other readers. Nadia