Jump to content

Directory/File Tree With Hyperlinks


rio38

Recommended Posts

I am trying to create a page that will list a directory/file tree and allow the user to click on the filename to download the file.

 

I have several directories with many files in each directory.  I would like the directory names listed with no hyperlink, but the filenames listed underneath the directory name and indented a bit with a hyperlink to the file location.

 

I did download a directory tree script from the web and it works fairly well, but I can not figure out how to get the hyperlink functionality working.

 

Can anyone help me with this?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/
Share on other sites

I apologize:

 

<?php			
function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
  $subdirectories=opendir($base);
  while (($subdirectory=readdir($subdirectories))!==false){
    $path=$base.$subdirectory;
    if (is_file($path)){
      if ($fileFunc!==null) $fileFunc($path);
    }else{
      if ($dirFunc!==null) $dirFunc($path);
      if (($subdirectory!='.') && ($subdirectory!='..')){
        traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
      }
      if ($afterDirFunc!==null) $afterDirFunc($path);
    }
  }
}

function outputPath($path){
  $level=substr_count($path,'/');
  for ($i=1;$i<$level;$i++) echo '   ';
  echo basename($path);
  echo "\n";
}
echo '<pre>';
traverseDirTree('./clientarea/','outputpath','outputpath');
echo '</pre>';
?>

I have not tested this, just catch the logic.

 

function outputPath($path){

//here just put the unnecessary path coming in $path with the your domain name

  $webPath=str_replace("/www/...","http://www.test.com/",$path);

  $level=substr_count($path,'/');

  for ($i=1;$i<$level;$i++) echo '  ';

  echo "<a href='" . $webPath . "'>" .  basename($path) . "</a>";

  echo "\n";

}

 

 

 

Got a bit carried away, sort of started redoing it (I just can't handle it unsorted or files b4 folders!), here's two ways, but they can be simplified somewhat...

STANDARD ECHO

<html>
<head>
<style type="text/css">
a:link { color:#414151; font-size: 11px; text-decoration: none; }
a:active { color:#414151; font-size: 11px; text-decoration: none; }
a:visited { color:#414151; font-size: 11px; text-decoration: none; }
a:hover { color:#5555dd; font-size: 11px; text-decoration: none; cursor: pointer;}
</style>

</head>
<body>

<?php

function getdir($base)
{
$f = array();
$d = array();
$dirs=opendir($base);
while (($dir=readdir($dirs))!==false)
{
	if( (strcmp($dir, '.') == 0) || (strcmp($dir, "..") == 0) )
	{
		continue;
	}
	elseif(is_file($base.$dir))
	{
		$f[] = $base.$dir;
	}
	else
	{
		$d[] = $base.$dir;
	}
}
sort($d);
sort($f);
return array_merge($d, $f);
}

function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null)
{
$dirs = getdir($base);

foreach($dirs as $path)
{
	if (is_file($path))
	{
		if ($fileFunc!==null)
		{
			$fileFunc($path);
		}
	}
	else
	{
		if ($dirFunc!==null)
		{
			$dirFunc($path);
		}

		if (($subdirectory!='.') && ($subdirectory!='..'))
		{
			traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
		}

		if ($afterDirFunc!==null)
		{
			$afterDirFunc($path);
		}

	}
}
}

function dodir($path)
{
$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++)
{
	echo '   ';
}
echo "<b>".basename($path)."</b><br>";
}

function dofile($path)
{

$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++)
{
	echo '   ';
}
echo "<a href='".$path."' target='dl'>".basename($path)."</a><br>";
}

traverseDirTree('testdir/','dofile','dodir');

?>
</body>
</html>

 

STRING OUTPUT

<html>
<head>
<style type="text/css">
a:link { color:#414151; font-size: 11px; text-decoration: none; }
a:active { color:#414151; font-size: 11px; text-decoration: none; }
a:visited { color:#414151; font-size: 11px; text-decoration: none; }
a:hover { color:#5555dd; font-size: 11px; text-decoration: none; cursor: pointer;}
</style>

</head>
<body>

<?php

function getdir($base)
{
$f = array();
$d = array();
$dirs=opendir($base);
while (($dir=readdir($dirs))!==false)
{
	if( (strcmp($dir, '.') == 0) || (strcmp($dir, "..") == 0) )
	{
		continue;
	}
	elseif(is_file($base.$dir))
	{
		$f[] = $base.$dir;
	}
	else
	{
		$d[] = $base.$dir;
	}
}
sort($d);
sort($f);
return array_merge($d, $f);
}

function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null)
{
$sret = "";

$dirs = getdir($base);

foreach($dirs as $path)
{
	if (is_file($path))
	{
		if ($fileFunc!==null)
		{
			$sret .= $fileFunc($path);
		}
	}
	else
	{
		if ($dirFunc!==null)
		{
			$sret .= $dirFunc($path);
		}

		if (($subdirectory!='.') && ($subdirectory!='..'))
		{
			$sret .= traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
		}

		if ($afterDirFunc!==null)
		{
			$sret .= $afterDirFunc($path);
		}

	}
}
return $sret;
}

function dodir($path)
{
$sret = "";
$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++)
{
	$sret .= '   ';
}
$sret .= "<b>".basename($path)."</b><br>";
return $sret;
}

function dofile($path)
{
$sret = "";
$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++)
{
	$sret .= '   ';
}
$sret .= "<a href='".$path."' target='dl'>".basename($path)."</a><br>";
return $sret;
}

$s = traverseDirTree('testdir/','dofile','dodir');

?>

<?php
echo $s;
?>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.