Get File Path Data in PHP

Submitted by:Jhon Smith

Date added:11 June, 2012


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.

Tags: php function

Code Snippet:

// pathinfo() constants parameter list
// PATHINFO_DIRNAME => directory name
// PATHINFO_BASENAME => name of file (w/out extension)
// PATHINFO_EXTENSION => file extension
// PATHINFO_FILENAME => file name w/ extension

$path = '/images/thumbs/my_avatar.gif';

//outputs '/images/thumbs/'
echo pathinfo($path, PATHINFO_DIRNAME);

//outputs 'my_avatar'
echo pathinfo($path, PATHINFO_BASENAME);

//outputs 'my_avatar.gif'
echo pathinfo($path, PATHINFO_FILENAME);

//outputs 'gif'
echo pathinfo($path, PATHINFO_EXTENSION);