Page 1 of 1

Quick scandir() question

Posted: Fri Apr 17, 2009 6:17 am
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

Re: Quick scandir() question

Posted: Fri Apr 17, 2009 6:55 am
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.

Re: Quick scandir() question

Posted: Fri Apr 17, 2009 8:12 am
by ldexterldesign
Cool, thought so. I'll just navigate around them when spitting out arrays in future then. Thanks for the response.

Re: Quick scandir() question

Posted: Fri Apr 17, 2009 10:28 am
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.

Re: Quick scandir() question

Posted: Fri Apr 17, 2009 12:38 pm
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

Re: Quick scandir() question

Posted: Tue Apr 21, 2009 11:28 am
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

Re: Quick scandir() question

Posted: Tue Apr 21, 2009 11:34 am
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.

Re: Quick scandir() question

Posted: Tue Apr 21, 2009 11:43 am
by ldexterldesign
Apologies. Noted*

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

Cheers,
L

Re: Quick scandir() question

Posted: Tue Apr 21, 2009 11:44 am
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