TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   List Directories, not files (http://www.talkphp.com/absolute-beginners/1965-list-directories-not-files.html)

buildakicker 01-15-2008 04:37 PM

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... :-P

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!

RobertK 01-15-2008 04:50 PM

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. :-)

buildakicker 01-15-2008 04:57 PM

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.

Alan @ CIT 01-15-2008 04:58 PM

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.

Wildhoney 01-15-2008 05:01 PM

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>';
}

buildakicker 01-15-2008 05:15 PM

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!

Alan @ CIT 01-15-2008 05:18 PM

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 :-D)

Alan

Salathe 01-15-2008 06:11 PM

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.

buildakicker 01-15-2008 06:16 PM

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?

RobertK 01-15-2008 06:32 PM

Quote:

Originally Posted by buildakicker (Post 8531)
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.

buildakicker 01-15-2008 10:51 PM

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>';
    } 


Village Idiot 01-15-2008 11:08 PM

PHP Code:

str_replace" "  "_"  $dir

That will replace every space in $dir with an underscore.

Aaron 01-15-2008 11:11 PM

Quote:

Originally Posted by RobertK (Post 8535)
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?

Village Idiot 01-15-2008 11:12 PM

Quote:

Originally Posted by Aaron (Post 8563)
Isn't MySQL licensed under the GPL?

I dont believe so.

RobertK 01-15-2008 11:26 PM

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.

buildakicker 01-15-2008 11:31 PM

Depends on your projects:

MySQL AB :: MySQL Licensing Policy

buildakicker 01-15-2008 11:37 PM

Quote:

Originally Posted by Village Idiot (Post 8562)
PHP Code:

str_replace" "  "_"  $dir

That will replace every space in $dir with an underscore.

Thanks for the help once again. ^^

EyeDentify 01-16-2008 10:02 AM

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

OldManRiver 02-22-2013 06:00 AM

Additional
 
All,

I entered thread, addressing similar item at:

http://www.talkphp.com/advanced-php-...ath-issue.html

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

nikeshoeshome 03-13-2013 08:08 AM

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


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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0