Displaying Folder & Filenames

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seib3184
Forum Newbie
Posts: 1
Joined: Sat Sep 03, 2005 12:02 pm

Displaying Folder & Filenames

Post by seib3184 »

I'm looking to add a pretty standard bread crumb trail to my website. ( Index -> Photos -> Cancun )

My directory is pretty well organized, IMO anyway. Each section is given its own folder, ie: Photos, Links, etc. In each folder are the pages that make up that section. I have a lot of pages so going through each one and actually hand coding a bread crumb trail is tedious work. On each page though, I've used a PHP include to place a header with my navigation. On that header I'd like to add some lines of code that would display the Index -> Folder_Name -> Filename (minus extension). Is this possible?

How would I go about doing it?

Thanks in advance for any help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like this:

Code: Select all

function buildBreadcrumb($separator = ' » ', $class = 'breadcrumb')
{
  $path = debug_backtrace();
  $path = substr((isset($path[1]) ? $path[1]['file'] : $path[0]['file']), strlen($_SERVER['DOCUMENT_ROOT']));
  $parts = explode(DIRECTORY_SEPARATOR, $path);
  $crumbs = array();
  for($i = 0; count($parts); $i++)
  {
    $url = implode('/', $parts);
    $part = array_pop($parts);
    $crumbs[] = array('text'=>($i == 0 ? preg_replace('#^(.*)\..*?$#','\\1',$part) : $part), 'url'=>$url.($i != 0 ? '/' : ''));
  }

  $crumbs = array_reverse($crumbs);
  $out = array();
  foreach($crumbs as $crumb)
  {
    $out[] = '<a href="'.$crumb['url'].'">'.$crumb['text'].'</a>';
  }
  $out = '<div class="'.$class.'">' . implode($separator,$out) . '</div>';
  return $out;
}


echo buildBreadcrumb();
Post Reply