08 September, 2010

Get only the path for a file

Sometimes it is required the get only the path that a file exists. In such cases following function could be of some help. I
hope this function helps someone like me

function getPath($path) {
if(substr($path, -1, 1) == '/') {
return $path;
}
else {
$pathArr = explode('/', $path);
$total = count($pathArr);
$end = $pathArr[$total - 1];
if(substr_count($end, '.') > 0) {
array_pop($pathArr);
}
$finalPath = implode('/', $pathArr);
return $finalPath;
}
}
?>

No comments: