Quick scandir() question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Quick scandir() question

Post by ldexterldesign »

Hi guys,

New here, so hello to all.

http://uk.php.net/scandir

Just wondering what the significance of the first two array elements are out of this function? I get:

Code: Select all

Array ( [0] => . [1] => .. [2] => ABBE.gif [3] => BRE.gif [4] => ECMK.gif [5] => IBG.gif [6] => thamesValley.gif )
... chucked out. But not quite sure why [0] and [1] exist, given I only have five files in the directory to scan for.

Thanks,
L
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Quick scandir() question

Post by requinix »

They're always there. In Windows, Unix, Mac... every system has those two directories everywhere.

. is the current directory and .. is the parent directory.
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Quick scandir() question

Post by ldexterldesign »

Cool, thought so. I'll just navigate around them when spitting out arrays in future then. Thanks for the response.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Quick scandir() question

Post by pickle »

glob() is another function you could use. It doesn't spit those out & has finer control over which types of files you display. Just FYI.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Quick scandir() question

Post by ldexterldesign »

pickle wrote:glob() is another function you could use. It doesn't spit those out & has finer control over which types of files you display. Just FYI.
Hey pickle, great thanks for this. I'll have a play this evening :wink:

L
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Quick scandir() question

Post by ldexterldesign »

Easy guys,

I'd like to get rid of the first element in this array (see script below). It outputs an initial list item I don't want, namely the filename

Code: Select all

..
referencing the the parent directory:

PHP

Code: Select all

       <?php
            // get template directory (wordpress)
            $templateDir = get_bloginfo(template_directory);
            // get current directory
            getcwd();
            // change to directory we want
            chdir('wp-content\themes\ldexterldesign\images\logos\accreditors\hoverOff');
            // make a variable of the path
            $cwd = getcwd();
            // get all the filenames
            $files = scandir($cwd);
            // count total files in directory
            $totalFiles = count($files);
            // get end directory from the $cwd
            $endDirectory = explode('\\', $templateDir);
            // spit out the filenames with img tag HTML
            echo '<ul id="accreditors">';
            for($i = 1; $i <= $totalFiles-1; $i++){
                echo '<li>';
                echo '<a href="#">';
                echo '<img src="';
                echo $templateDir;
                echo '/images/logos/accreditors/hoverOff/';
                echo $files[$i];
                echo '"/>';
                echo '</a>';
                echo '</li>';
                }
                echo '</ul>';
        ?>
HTML

Code: Select all

<ul id="accreditors">
[i]  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/.."/></a></li>[/i]
  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/ABBE.gif"/></a></li>
  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/BRE.gif"/></a></li>
  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/ECMK.gif"/></a></li>
  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/IBG.gif"/></a></li>
  <li><a href="#"><img src="http://localhost/wordpress/wp-content/themes/ldexterldesign/images/logos/accreditors/hoverOff/thamesValley.gif"/></a></li>
</ul>
I'm keen to use the glob() function as @pickle kindly pointed out, but am at a loss as to how and where to implement it (even if it's the correct solution at all!)

Thanks to anyone that can help.

Cheers,
L
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Quick scandir() question

Post by pickle »

If you have a new question, even if its related, please post it in a new thread. If you edit your most recent post to have a relevant subject, I can move it to a new thread for you.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Quick scandir() question

Post by ldexterldesign »

Apologies. Noted*

I'll make a fresh post and delete this last comment.

Cheers,
L
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Quick scandir() question

Post by ldexterldesign »

You might have to delete it for me @pickle!

I've opened up a fresh post here: http://is.gd/tHhC

Thanks,
L
Post Reply