Get File Path Data in PHP


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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
  7.  
  8. $path = '/images/thumbs/my_avatar.gif';
  9.  
  10. //outputs '/images/thumbs/'
  11. echo pathinfo($path, PATHINFO_DIRNAME);
  12.  
  13. //outputs 'my_avatar'
  14. echo pathinfo($path, PATHINFO_BASENAME);
  15.  
  16. //outputs 'my_avatar.gif'
  17. echo pathinfo($path, PATHINFO_FILENAME);
  18.  
  19. //outputs 'gif'
  20. echo pathinfo($path, PATHINFO_EXTENSION);
  21. ?>

URL: http://www.apphp.com/index.php?snippet=php-get-file-path-data

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.