Mapstructuur weergeven
Auteur: Koen - 07 januari 2009 - 18:12 - Gekeurd door: Stijn - Hits: 4789 - Aantal punten: (0 stemmen)
Met behulp van deze functies kan je de mapstructuur van een bepaalde map in een array steken, en daarna eventueel weergeven in een overzichtelijke structuur.
(Werkt recursief)
Veel plezier
Code:
<?php
/**
* Function viewDir()
* Create the array which will contain the list of all the directories/files in the specified directory.
*
* @author Koen <koen@sitemasters.be>
* @version 1.1
*
* @param string $sDir Name of the directory, eg: images
* @param array $aExclude array containing the elements to be excluded eg: array('.', '..')
*
* Return values:
* false on failure
* array containing the list of the directories/files.
*
* Function makeTree()
* Generates the treeview with the array created by "viewDir()"
*
* @author Koen <koen@sitemasters.be>
* @version 1.0
*
* @param array $aDir output of "viewDir()" function
* @param int $iCurrent current margin-left [optional, default: 0]
*
* Return values:
* false on failure
* ready-to-use treeview!
*
*/
function viewDir($sDir, & $aExclude)
{
if(!is_dir($sDir))
{
return false;
}
$aList = scandir($sDir);
$aReturn = array();
foreach($aList as $iKey => $sValue)
{
if(!in_array($sValue, $aExclude))
{
if(is_dir($sDir.'/'.$sValue))
{
$aReturn[$sValue] = viewDir($sDir.'/'.$sValue, $aExclude);
} else
{
$aReturn[] = $sValue;
}
}
}
return $aReturn;
}
function makeTree($aDir, $iCurrent = 0)
{
$iExpandBy = 20;
$sReturn = '';
if(!is_array($aDir))
{
return false;
}
foreach($aDir as $iKey => $sValue)
{
$sCurrentPX = $iCurrent .'px';
if(is_array($sValue))
{
$iNewCurrent = $iCurrent + $iExpandBy;
$sReturn .= '<span style="margin-left: '.$sCurrentPX.';">'.$iKey.'</span><br />'."\n";
$sReturn .= makeTree($sValue, $iNewCurrent);
} else
{
$sReturn .= '<span style="margin-left: '.$sCurrentPX.';">'.$sValue.'</span><br />'."\n";
}
}
return $sReturn;
}
// example of usage:
$aExclude = array('.', '..');
$aDir = viewDir('dir', $aExclude);
echo makeTree($aDir);
?>
<?php
/**
* Function viewDir()
* Create the array which will contain the list of all the directories/files in the specified directory.
*
* @author Koen <koen@sitemasters.be>
* @version 1.1
*
* @param string $sDir Name of the directory, eg: images
* @param array $aExclude array containing the elements to be excluded eg: array('.', '..')
*
* Return values:
* false on failure
* array containing the list of the directories/files.
*
* Function makeTree()
* Generates the treeview with the array created by "viewDir()"
*
* @author Koen <koen@sitemasters.be>
* @version 1.0
*
* @param array $aDir output of "viewDir()" function
* @param int $iCurrent current margin-left [optional, default: 0]
*
* Return values:
* false on failure
* ready-to-use treeview!
*
*/
function viewDir( $sDir , & $aExclude )
{
{
return false ;
}
$aList = scandir( $sDir ) ;
foreach ( $aList as $iKey => $sValue )
{
{
{
$aReturn [ $sValue ] = viewDir( $sDir . '/' . $sValue , $aExclude ) ;
} else
{
$aReturn [ ] = $sValue ;
}
}
}
return $aReturn ;
}
function makeTree( $aDir , $iCurrent = 0 )
{
$iExpandBy = 20 ;
$sReturn = '' ;
{
return false ;
}
foreach ( $aDir as $iKey => $sValue )
{
$sCurrentPX = $iCurrent . 'px' ;
{
$iNewCurrent = $iCurrent + $iExpandBy ;
$sReturn .= '<span style="margin-left: ' . $sCurrentPX . ';">' . $iKey . '</span><br />' . "\n " ;
$sReturn .= makeTree( $sValue , $iNewCurrent ) ;
} else
{
$sReturn .= '<span style="margin-left: ' . $sCurrentPX . ';">' . $sValue . '</span><br />' . "\n " ;
}
}
return $sReturn ;
}
// example of usage:
$aExclude = array ( '.' , '..' ) ; $aDir = viewDir( 'dir' , $aExclude ) ;
?>
Download code (.txt)
Stemmen
Niet ingelogd.