By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

list folders script

P: 12
Hi

Does anyone know how to make a script that list the folders in a specific directory, but when the list is made it must be possible to click on it like a link so people can be redirected, but not to the folder, but to a special file inside the folder.

example:
- list the names on the folders from root
- the names appear in a link form (<a href=' ' </a>) on my php page
- when i click i shall be redirected to one-of-the-folder-names/index.php

thank you for paying attention, i hope you can help
Feb 15 '08 #1
Share this Question
Share on Google+
8 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Happen to have one of those 'on-the-shelf'
[php]
<?php
// Your start directory
getDirectory('./');

function getDirectory( $path = '.', $level = 0 )
{
// Directories to ignore when listing output.
$ignore = array( '.', '..' );

// Open the directory to the handle $dh
$dh = @opendir( $path );

// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) )
{
// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) )
{
// Indent spacing for better view
$spaces = str_repeat( '&nbsp;', ( $level * 5 ) );

// Show directories only
if(is_dir( "$path/$file" ) )
{
// Re-call this same function but on a new directory.
// this is what makes function recursive.
echo "$spaces<a href='$path/$file/index.php'>$file</a><br />";
getDirectory( "$path/$file", ($level+1) );
}
}
}
// Close the directory handle
closedir( $dh );
}
?>
[/php]
Ronald
Feb 15 '08 #2

P: 12
Happen to have one of those 'on-the-shelf'
[php]
<?php
// Your start directory
getDirectory('./');

function getDirectory( $path = '.', $level = 0 )
{
// Directories to ignore when listing output.
$ignore = array( '.', '..' );

// Open the directory to the handle $dh
$dh = @opendir( $path );

// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) )
{
// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) )
{
// Indent spacing for better view
$spaces = str_repeat( '&nbsp;', ( $level * 5 ) );

// Show directories only
if(is_dir( "$path/$file" ) )
{
// Re-call this same function but on a new directory.
// this is what makes function recursive.
echo "$spaces<a href='$path/$file/index.php'>$file</a><br />";
getDirectory( "$path/$file", ($level+1) );
}
}
}
// Close the directory handle
closedir( $dh );
}
?>
[/php]
Ronald
Hi again

i noticed this:
// Directories to ignore when listing output.
$ignore = array( '.', '..' );

using this script as provided shows some of my folders which lies inside the folder which names is displayed. What is the kind of "code" that i have to add to $ignore = array( '.', '..' ); to ignore showing these folders?

Example:
Name-on-folder
- foldder-inside-Name-on-folder


Noticed that adding "folder-inside" will remove showing the folder, but because i have so many folders, i want to write a generel thing that ignores all the folders
$ignore = array( '.', '..', "folder-inside" );
Feb 19 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Just take out the recursive part, i.e. line 28
[php]getDirectory( "$path/$file", ($level+1) ); [/php]
anf it will only show the folders under the dir you specified.

Ronald
Feb 19 '08 #4

P: 2
Hi,

Is there a way to display the listing alphabetically?

Regards
Imran
Jul 31 '08 #5

100+
P: 310
Nice little script. Would be only a small amount of effort to modify it (with a third argument) to let the user specify how many levels down in the directory tree the function (and it's recursive calls) would go.

To sort the results, I would suppose I would modify all the echo statements so that instead of directly printing out the results to the remote user as the "dh" list is retrieved, I would store the results intermediately in arrays. Then when all the files and subdirectories have been traversed, sort the arrays one by one alphabetically and do the "echo" printouts to the user.
Jul 31 '08 #6

P: 2
Nice little script. Would be only a small amount of effort to modify it (with a third argument) to let the user specify how many levels down in the directory tree the function (and it's recursive calls) would go.

To sort the results, I would suppose I would modify all the echo statements so that instead of directly printing out the results to the remote user as the "dh" list is retrieved, I would store the results intermediately in arrays. Then when all the files and subdirectories have been traversed, sort the arrays one by one alphabetically and do the "echo" printouts to the user.
Do you any sample code I can use?
Aug 1 '08 #7

P: 1
Thanks for sharing your script, Roverdonk. How do I get it to list the folders / links in reverse order, so that the newest is on the top?

Thanks.

R
Jan 19 '10 #8

dlite922
Expert 100+
P: 1,584
see php.net/glob

and php.net/filemtime

get the directory files with glob. store the filename and the file modified time in an array. Then reverse sort the array: php.net/arsort

Isn't PHP great? The manual gives you all these fantastic functions to do anything you want.



Dan
Jan 19 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.