rio38 Posted September 25, 2007 Share Posted September 25, 2007 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 More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 could you like to post the script? Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354887 Share on other sites More sharing options...
rio38 Posted September 25, 2007 Author Share Posted September 25, 2007 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354891 Share on other sites More sharing options...
rio38 Posted September 25, 2007 Author Share Posted September 25, 2007 rarebit, I can't send private messages yet, but you will be a lifesaver if you could help me out. Also, I really do not want it to display the '.' and '..' in the list if possible. Thank you in advance. Rio38 Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354919 Share on other sites More sharing options...
md_dev Posted September 25, 2007 Share Posted September 25, 2007 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"; } Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354930 Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 just so you know you can accomplish the same idea with a public ftp account, php can even handle the logging in of it. Instead of trying to manually display the files and so forth Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354931 Share on other sites More sharing options...
rio38 Posted September 25, 2007 Author Share Posted September 25, 2007 CoolDude, I am open to any suggestions. How would PHP handle the login for an FTP account? rarebit: I will try this out. Thanks and let me know if you can expand on it. Thanks. Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354935 Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 find your own answer: http://us3.php.net/manual/en/ref.ftp.php Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-354937 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 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> Link to comment https://forums.phpfreaks.com/topic/70622-directoryfile-tree-with-hyperlinks/#findComment-355072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.