Hi,
i want have created a directory browser using PHP, where user can browse between directories.
Now i want to implement breadcrumb. Like if we are inside say "folder01" then URL of the page is
http://www.xyz.com?file=folder01
and its breadcrumb would be
<ul class="breadcrumb">
<li><a href="index.php">Home</home>
<li><a href="http://www.xyz.com?file=folder01">Folder 01</home>
</ul>
similarly if we are one step deep in "folder01" then page url is
http://www.xyz.com?file=folder01/folder02
and it desired bread crumb would be
<ul class="breadcrumb">
<li><a href="index.php">Home</home>
<li><a href="http://www.xyz.com?file=folder01">Folder 01</home>
<li><a href="http://www.xyz.com?file=folder01/folder02">Folder 02</home>
</ul>
Here is the given code which is working for me.
$root = __DIR__;
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
$parent = realpath($file);
$i = 0;
while ($parent) {
if ($directory == $parent) return true;
if ($parent == dirname($parent) || !$recursive) break;
$parent = dirname($parent);
}
return false;
}
$path = null;
if (isset($_GET['file'])) {
$path = $_GET['file'];
if (!is_in_dir($_GET['file'], $root)) {
$path = null;
} else {
$path = '/'.$path;
}
}
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
if ($path) echo '<a href="?file='.urlencode(substr(dirname($root.$path), strlen($root) + 1)).'">..</a><br />';
foreach (glob($root.$path.'/*') as $file) {
$file = realpath($file);
$link = substr($file, strlen($root) + 1);
echo '<a href="?file='.urlencode($link).'">'.basename($file).'</a><br />';
}