03-11-2008, 09:40 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
Read Directory Function
Most of you developing websites or having portfolios, this function may come in use in your PHP endeavors.
Example: Directory Quick Browse Script
Added Note: If you're using anything less than php version 5.0.2, or you're getting an error using the code, add this in as well.
PHP Code:
if(!defined('PHP_EOL'))
{
define('PHP_EOL', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n"));
}
And here's the function :)
PHP Code:
function read_directory($directory)
{
if(is_dir($directory))
{
echo "<ul>" . PHP_EOL;
if($handle = @opendir($directory))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != "..")
{
if(is_dir($directory . DIRECTORY_SEPARATOR . $file))
{
$directories[] = $file;
}
else
{
$files[] = $file;
}
}
}
if(!empty($directories))
{
sort($directories);
foreach($directories as $sub_directory)
{
echo "<li>" . $sub_directory . DIRECTORY_SEPARATOR . "</li>";
echo "<ul>";
read_directory($directory . DIRECTORY_SEPARATOR . $sub_directory);
echo "</ul>";
}
}
if(!empty($files))
{
sort($files);
foreach($files as $file)
{
echo "<li><a href=\"" . $directory . DIRECTORY_SEPARATOR . $file . "\">" . $file . "</a></li>" . PHP_EOL;
}
}
@closedir($handle);
echo "</ul>" . PHP_EOL;
}
}
}
To use it, just simply write the following code:
PHP Code:
read_directory('portfolio');
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|