• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Get the filename extension with PHP

Get the filename extension with PHP

I’ve covered how to get the filename and directory name from a full path with PHP before and did include a section which happened to include how to get the filename extension as well, but I continue to find myself unable to remember the name of the function so I’ve put that part of the previous post into this new one with a title specific to finding the extension.

Get the directory name, filename and extension with pathinfo()

PHP’s pathinfo() function returns an associative array containing the basename, dirname, extension and (from PHP 5.2.0) the filename without the extension.

print_r(pathinfo($path));

will echo

Array
(
    [dirname] => /var/www/mywebsite/htdocs/images
    [basename] => myphoto.jpg
    [extension] => jpg
    [filename] => myphoto
)

To get just the extension you could do something like this, assuming you would rather save the extension to a separate variable rather than use the returned associative array:

$pathinfo = pathinfo($path);
$extension = $pathinfo['extension'];

Check Out These Related posts:

  1. Generate thumbnails with PHP
  2. Which directory is that bash script in?
  3. Remove extension from a filename with PHP
  4. Fix the mbstring PHP extension not found error in phpMyAdmin

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap