TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
View Poll Results: What is your PHP "level" of Expertise?
Beginner 4 30.77%
Intermedia 7 53.85%
Advanced 2 15.38%
Voters: 13. You may not vote on this poll

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-15-2008, 04:37 PM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Asterix 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!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 04:50 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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
RobertK is offline  
Reply With Quote
The Following User Says Thank You to RobertK For This Useful Post:
buildakicker (01-15-2008)
Old 01-15-2008, 04:57 PM   #3 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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.
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 04:58 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
buildakicker (01-15-2008)
Old 01-15-2008, 05:01 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
buildakicker (01-15-2008)
Old 01-15-2008, 05:15 PM   #6 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 05:18 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
buildakicker (01-15-2008)
Old 01-15-2008, 06:11 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
buildakicker (01-15-2008)
Old 01-15-2008, 06:16 PM   #9 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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?
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 06:32 PM   #10 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Quote:
Originally Posted by buildakicker View Post
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
RobertK is offline  
Reply With Quote
Old 01-15-2008, 10:51 PM   #11 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

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>';
    } 
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 11:08 PM   #12 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

PHP Code:
str_replace" "  "_"  $dir
That will replace every space in $dir with an underscore.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-15-2008, 11:11 PM   #13 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Quote:
Originally Posted by RobertK View Post
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?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-15-2008, 11:12 PM   #14 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
Isn't MySQL licensed under the GPL?
I dont believe so.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-15-2008, 11:26 PM   #15 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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
RobertK is offline  
Reply With Quote
Old 01-15-2008, 11:31 PM   #16 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Depends on your projects:

MySQL AB :: MySQL Licensing Policy
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-15-2008, 11:37 PM   #17 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Big Grin

Quote:
Originally Posted by Village Idiot View Post
PHP Code:
str_replace" "  "_"  $dir
That will replace every space in $dir with an underscore.
Thanks for the help once again.
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-16-2008, 10:02 AM   #18 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Smile

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.
EyeDentify is offline  
Reply With Quote
Old 02-22-2013, 06:00 AM   #19 (permalink)
The Wanderer
 
OldManRiver's Avatar
 
Join Date: Feb 2013
Posts: 7
Thanks: 0
OldManRiver is on a distinguished road
Default Additional

All,

I entered thread, addressing similar item at:

Object to fix Firefox fullpath issue!

I have to have the mvc class popup object that shows the dialog with folders showing as icons, for my solution.

How would you handle that?

Thanks!

OMR
OldManRiver is offline  
Reply With Quote
Old 03-13-2013, 08:08 AM   #20 (permalink)
The Wanderer
 
Join Date: Mar 2013
Posts: 13
Thanks: 0
nikeshoeshome is on a distinguished road
Default

Nike Free 3.0 Shoes barefoot series of running shoes is one of the last couple of years, most the Nike fans favorite Nike Free 3.0 V4 running shoes.The series generally forefoot Waffle husband outside shading road design, has raised friction block has extremely suitable for running grip, and can be dispersed impact, make running more comfortable. The heel BRS1000 carbon fiber rubber, more wear-resistant, good grip, but the intense friction will leave a black mark on the ground.If Nike Free 3.0 Womens flexibility rating, 0.0 for barefoot running, 5.0 for ordinary running shoes. Nike Free Run Shoes running shoes introduced a FREE 3.0, Nike Free 3.0 V3 , FREE 7.0 FREE EVERYDAY FREE RUN + FREE style.nikefree30shoessale130313
nikeshoeshome is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design