Get File Path Data in PHP

You may retrieve all needed file path data using PHP's built-in function pathinfo. You don't need to create your own functions or use regular expressions to get this info. It was already been created for this purpose.

[View All Snippets]
Show Plain Text »
  1. <?php
  2. // pathinfo() constants parameter list
  3. // PATHINFO_DIRNAME  => directory name
  4. // PATHINFO_BASENAME => name of file (w/out extension)
  5. // PATHINFO_EXTENSION => file extension
  6. // PATHINFO_FILENAME   => file name w/ extension
  8. $path = '/images/thumbs/my_avatar.gif';
  10. //outputs '/images/thumbs/'
  11. echo pathinfo($path, PATHINFO_DIRNAME);
  13. //outputs 'my_avatar'
  14. echo pathinfo($path, PATHINFO_BASENAME);
  16. //outputs 'my_avatar.gif'
  17. echo pathinfo($path, PATHINFO_FILENAME);
  19. //outputs 'gif'
  20. echo pathinfo($path, PATHINFO_EXTENSION);
  21. ?>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory

Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.