Use this php class to create a sitemap for your site, or create a links menu.
The function is recursive, so it will read the content of the directories, and the subdirectories in them.
Here is the class file:
Code:
<?php
<?php
class file2menu {
////////////////////////////////////////////////////
// File to menu converter class
//
// Version 1.00
//
// This class is creating an HTML menu from the files that
// exist in a directory, and its subdirectories
//
// Author: Nick Papanotas
//
// License: GPL, see LICENSE
// Support: You can post your questions/bugs or anything else
// to http://www.webdigity.com/
//
////////////////////////////////////////////////////
/*
The directory that we want to create the listing for. Should be relative to the web site’s root path
@var string
*/
var $directory;
/*
The file types that will be displayed in the menu
@var string
*/
var $acceptedFileTypes = array (‘php’,‘html’,‘htm’);
/*
Should we display the file extension to the menu?
@var string
*/
var $displayExt = false;
/*
Created by the preparse() function
@var array
*/
var $listArray;
/*********************************************************
* PUBLIC FUNCTIONS *
*********************************************************/
/*
A constructor function
@access public
@return bool
*/
function file2menu($directory)
{
$this->directory = $directory;
if ( empty ( $this->directory ))
die ( ‘Directory not selected’ );
else
{
$this->directory = substr($this->directory,0,1) != ‘/’?‘/’.$this->directory:$this->directory;
$this->directory = substr($this->directory,-1) == ‘/’?substr($this->directory,0,-1):$this->directory;
$this->directory = $_SERVER['DOCUMENT_ROOT']. $this->directory;
if ( !is_dir($this->directory ))
die ( $this->directory . ‘ is not a directory’);
}
return true;
}
/*
Creates the $this->listArray array. You don’t have to use this function
This function is a modified version of http://fundisom.com/phparadise/php/directories/recursive_directory_listing
@access private/puplic
@return array
*/
function preparse($dir = ”)
{
$dir = empty($dir)?$this->directory:$dir;
$dirlist = opendir($dir);
while ($file = readdir ($dirlist))
{
if ($file != ‘.’ && $file != ‘..’)
{
$newpath = $dir.‘/’.$file;
$level = explode(‘/’,$newpath);
if (is_dir($newpath))
{
$mod_array[] = array(
‘level’=>count($level)-1,
‘path’=>$newpath,
‘relPath’ => substr($newpath,strlen($_SERVER['DOCUMENT_ROOT'])),
‘name’=>end($level),
‘type’=>‘dir’,
‘mod_time’=>filemtime($newpath),
‘content’=>$this->preparse($newpath));
}else{
$ext = array_pop(explode(‘.’,$file));
if ( in_array ($ext,$this->acceptedFileTypes))
{
$end = end($level);
$mod_array[] = array(
‘level’=>count($level)-1,
‘name’=>$this->displayExt?$end:substr($end,0,(strlen($end) – strlen($ext)-1)),
‘relPath’ => substr($newpath,strlen($_SERVER['DOCUMENT_ROOT'])),
‘type’=>‘file’,
‘mod_time’=>filemtime($newpath),
’size’=>filesize($newpath));
}
}
}
}
closedir($dirlist);
$this->listArray = $mod_array;
return $mod_array;
}
/*
A recursive function that displays a menu. If you want to modify the look of the autogenerated menu, this is the function you have to change
@access public
@return NULL
*/
function DisplayMenu($list=”,$displayHeaderFooter = true)
{
if ( empty($this->listArray))
$this->preparse();
$list = empty($list)?$this->listArray:$list;
$levelsSub = count(explode(‘/’,$_SERVER['DOCUMENT_ROOT']));
if ( $displayHeaderFooter )
echo ‘<pre>’;//You can put some html header here
foreach ( $list as $item )
{
for ( $i=$levelsSub; $i<$item['level'];$i++)
echo “\t”;
if ( $item['type'] == ‘file’ )
{
//Here we parse files :
echo ‘<a href=”‘.$item['relPath'].‘”>’.$item['name'].‘</a><br />’;
}else{
//Here we parse directories:
echo ‘<b>’.$item['name'].‘</b><br />’;
if ( !empty($item['content']))
echo $this->DisplayMenu($item['content'],false);
}
}
if ( $displayHeaderFooter )
echo ‘</pre>’;//You can put some html footer here
}
/*
Saves the output of the DisplayMenu function to a file.
@access public
@return bool
*/
function SaveAs ( $fileName )
{
if (!$fp = @fopen ($fileName, ‘w’))
die ( ‘Can\’t open file for writing.’);
ob_start();
$this->DisplayMenu();
$data = ob_get_clean();
if ( !@fwrite($fp, $data))
die( ‘Can\’t write to file.’);
if ( !@fclose($fp))
die ( ‘Cam\’t close file.’);
return true;
}
}
?>
To use the function you just need something like this :
<?php
$dir = new file2menu(‘/myDirectory’);
$dir->DisplayMenu();
You can also use the SaveAs method to save the menu in a file :
<?php
$dir = new file2menu(‘/myDirectory’);
$dir->SaveAs(‘FileToSave.html’);
If you have any comments, questions regarding this class, please post a topic here.
This class originally posted to webdigity’s code library.