 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
|
View Poll Results: What is your PHP "level" of Expertise?
|
|
Beginner
|
 
|
4 |
30.77% |
|
Intermedia
|
 
|
7 |
53.85% |
|
Advanced
|
 
|
2 |
15.38% |
 |
 |
|
 |
01-15-2008, 03:37 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
List Directories, not files
Hello all,
Great website here. I hope I can share and help here, as well as get help as I learn...
I am trying to list just the directories under a certain folder. I don't want the files, just directories. Currently I have this:
PHP Code:
<?php
if ($handle = opendir('../categories/')) {
/* loop through directory. */
while (false !== ($dir = readdir($handle))) {
echo '<option value='.$dir.'>'.$dir.'</option>';
}
closedir($handle);
}
?>
Right now it shows a drop down select option with this at the beginning:
PHP Code:
.
..
Directory1
Directory2
Any ideas?
Thanks!
|
|
|
|
01-15-2008, 03:50 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
|
Personally I'd do it this way:
PHP Code:
foreach(glob('../categories/*', GLOB_ONLYDIR) as $dir) { echo '<option value='.$dir.'>'.$dir.'</option>'; }
My level is roughly halfway between intermediate and advanced, depending upon which subject you're after. 
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
|
|
|
|
|
The Following User Says Thank You to RobertK For This Useful Post:
|
|
01-15-2008, 03:57 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Thanks for the reply. However, I would like to just display the Directory Name, not with ../categories/ in front of it. I found if I do:
PHP Code:
if ($handle = opendir('../categories/')) { /* loop through directory. */ while (false !== ($dir = readdir($handle))) { if($dir != ".." && $dir != "."){ echo '<option value='.$dir.'>'.$dir.'</option>'; } } closedir($handle); }
It hides the "." and ".." , how can I hide the ../categories/ with your example? It seems to be a simple, quicker approach.
|
|
|
|
01-15-2008, 03:58 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
I'd suggest using the is_dir() function in your while() loop.
Eg:
PHP Code:
<?php
if ($handle = opendir('../categories/')) {
/* loop through directory. */
while (false !== ($dir = readdir($handle))) {
if (is_dir($dir))
{
echo '<option value='.$dir.'>'.$dir.'</option>';
}
}
closedir($handle);
}
?>
In theory, this should skip anything that isn't a directory.
Note: '.' and '..' are considered directories so you might want to strip them as well
Alan.
|
|
|
|
The Following User Says Thank You to Alan @ CIT For This Useful Post:
|
|
01-15-2008, 04:01 PM
|
#5 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
How about something such as the following. You could use regular expressions, but a normal string replace is much faster in this case.
php Code:
foreach(glob('../categories/*', GLOB_ONLYDIR ) as $dir) { $dir = str_replace('../categories/', '', $dir); echo '<option value='. $dir. '>'. $dir. '</option>'; }
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
01-15-2008, 04:15 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Thanks Wildhoney. That's nice and small and quick.
I am in the process of creating a flatfile cms of a sort for an intranet that has no access to a database, well I guess there is an Oracle db, but I don't understand how to use it... no one knows... I am trying to create a post function that will allow users to post information under directories. That is why I am trying to get just the directories.
Thanks!
|
|
|
|
01-15-2008, 04:18 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Assuming your PHP build supports it, you coud always use SQLite instead - a ready to go database server built in to PHP
More details: TalkPHP - Introduction to SQLite (yes, blatent self-promotion  )
Alan
|
|
|
|
The Following User Says Thank You to Alan @ CIT For This Useful Post:
|
|
01-15-2008, 05:11 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
|
How about :
PHP Code:
foreach(glob('../categories/*', GLOB_ONLYDIR) as $dir)
{
$dir = basename($dir);
echo '<option value="', $dir, '">', $dir, '</option>';
}
We use GLOB_ONLYDIR as in Wildhoney's example to grab the paths to directories within ../categories, then grab only the directory name (using basename()), before outputting the option HTML element.
Aside: Note the use of commas to separate expressions for the echo statement, rather than concatenating to a string.
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
01-15-2008, 05:16 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
No php5... someday they MIGHT update. Salathe... even less code! This is great. I went from 168 to 121 characters.
Have any of you used Gladius? Or FFDB?
|
|
|
|
01-15-2008, 05:32 PM
|
#10 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
|
Quote:
Originally Posted by buildakicker
Have any of you used Gladius? Or FFDB?
|
Nope. I refuse to use project licensed with viral licenses like the GPL. The copyleft forces me to adopt a GPL license, when I have absolutely no desire to use the GPL. No good. GPLv3 removes any ability I had before to use a GPL licensed project in compiled form too, like a library. Free implies freedom of use as well as "no cost".
Haven't seen/heard-of FFDB so ... nope. Gladius, heard of it by vague reference.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
|
|
|
|
01-15-2008, 09:51 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Salathe: I just noticed that when I link to the $dir that is passed, I get only the first word in the directory. How do I go about making sure there are no spaces, and if there is, filling it with a _ when new directories are created from my form?
PHP Code:
$path = "../categories/"; foreach(glob('../categories/*', GLOB_ONLYDIR) as $dir){ $dir = basename($dir); echo '<li><a href='.$path.''. $dir .'>'.$dir.'</a></li>'; }
|
|
|
|
01-15-2008, 10:08 PM
|
#12 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,298
Thanks: 17
|
PHP Code:
str_replace( " " , "_" , $dir)
That will replace every space in $dir with an underscore.
|
|
|
|
01-15-2008, 10:11 PM
|
#13 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
Quote:
Originally Posted by RobertK
Nope. I refuse to use project licensed with viral licenses like the GPL. The copyleft forces me to adopt a GPL license, when I have absolutely no desire to use the GPL. No good. GPLv3 removes any ability I had before to use a GPL licensed project in compiled form too, like a library. Free implies freedom of use as well as "no cost".
Haven't seen/heard-of FFDB so ... nope. Gladius, heard of it by vague reference.
|
Isn't MySQL licensed under the GPL?
|
|
|
01-15-2008, 10:12 PM
|
#14 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,298
Thanks: 17
|
Quote:
Originally Posted by Aaron
Isn't MySQL licensed under the GPL?
|
I dont believe so.
|
|
|
|
01-15-2008, 10:26 PM
|
#15 (permalink)
|
|
The Addict
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
|
Aaron,
I'm not sure, but even if it is the implementation is a binary executable or library bound into PHP, and our scripts are just data and exempt. (WHEW!) If it gets linked into (compiled) or bound into your application you must use the GPL too. Reference this part of the FAQ plus the following two sections. This one is ugly too. And this, and the one immediately after. Unfortunately there is no end to this.
So, it's just a lot simpler to avoid all GPL'ed code for non-GPL projects. No headaches that way.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
|
|
|
|
01-15-2008, 10:31 PM
|
#16 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
|
|
|
|
01-15-2008, 10:37 PM
|
#17 (permalink)
|
|
The Acquainted
Join Date: Jan 2008
Posts: 119
Thanks: 21
|
Quote:
Originally Posted by Village Idiot
PHP Code:
str_replace( " " , "_" , $dir)
That will replace every space in $dir with an underscore.
|
Thanks for the help once again. 
|
|
|
|
01-16-2008, 09:02 AM
|
#18 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
|
Hey i just had to put my little contribution to the tread.
I borrowed some of WildHoneys code (sorry about that :) ).
php Code:
$path = '../categories/'; $path_len = strlen($path); foreach(glob($path . '*', GLOB_ONLYDIR ) as $dir) { $dir = substr($dir, 0, - $path_len); echo '<option value='. $dir. '>'. $dir. '</option>'; }
My hopes is that going this way the lenght of the path would be recognized and cut of from the $dir variable.
I was unsure about the last two parameters in substr() at time of writing this quickly, but i think you get the idea.
/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|