473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

list folders script

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
8 21294
ronverdonk
4,258 Expert 4TB
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
7effrey
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
4,258 Expert 4TB
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
Hi,

Is there a way to display the listing alphabetically?

Regards
Imran
Jul 31 '08 #5
coolsti
310 100+
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
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
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
1,584 Expert 1GB
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

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

Similar topics

5
by: steve | last post by:
Hi, I finally figured out the best way to synch local and remote script folders, containing many php script files. What I used to do before was try to ftp all the changed files, etc. which was...
3
by: Eclectic | last post by:
I have a script that reads all directory names within a specific location (only containing folders), then lists all jpeg files in those directories. Here is my script: function...
5
by: Konrad L. M. Rudolph | last post by:
I hope this is the right NG, please notify me if not. I have got a problem with the "recent files" list of the start page in VS.NET: yesterday I created a new projekt which has the same name as...
6
by: Dave Hopper | last post by:
Hi I am using the following SQL to retrieve a value in a list box using a unique ID held in the list box call cntID. The list box is used on an order form to list appointments that have been...
7
by: Dave Hopper | last post by:
Hi I posted a question recently regarding problems I am having getting a value from a list box to use in a query. I got a lot of help, for which I thank you and it's nearly working! But I need...
3
by: sva | last post by:
Using C# for an application in which I am working on, I need to display the available shared folders on the computer that's running the application. The shared folder paths need to be in UNC format...
1
by: lecnac | last post by:
Sorry for the repost. I must have done something wrong when I tried to post my reply (I can't seem to find it). Anyway, I'd really appreciate any help that anyone could provide. My issue is...
2
by: peirob2006 | last post by:
Is there a way to list only the folders of a certain path in a List box? If so...how do I go about doing that. What I want to do is list all the folders in C:\Folder1\ and Display the Names of...
2
by: Mike P | last post by:
I need to find all the folders at a certain location, and then once I have this list of folders I need to add some of them to a collection and then show the names of those folders on screen (there...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.