2

I have a script that scans a folder and put in an array the file names it contains. Then I shuffle the array and display the file names.

Like this:

$count=0;
$ar=array();
$i=1;
$g=scandir('./images/');

foreach($g as $x)
{
    if(is_dir($x))$ar[$x]=scandir($x);
    else 
    { 
        $count++;
        $ar[]=$x;   
    }
}
shuffle($ar);

while($i <= $count)
{
    echo $ar[$i-1];
    $i++;
}
?>

It works well but for some reason I get something like this:

  • fff.jpg
  • ccc.jpg
  • Array
  • nnn.jpg
  • ttt.jpg
  • sss.jpg
  • bbb.jpg
  • Array
  • eee.jpg

Of course, the order changes when I refresh the page because of the shuffle I did but among 200 filenames I always get these 2 "Array" somewhere in the list.

What could it be?

Thank you

8
  • 2
    glob() is much more fun Commented May 13, 2015 at 23:44
  • @Dagon blob blob I think that would be fish :) Do you mean glob ? Commented May 13, 2015 at 23:47
  • I'll look for glob, thank you. In the meantime, I would like to know what is going wrong with the script as it is (the first one). Here in my example, I just "echo" the filenames to make it simple but I need more than that. So I need to store the names in an array for a later use. I believe that, glob or not, I will end up with the exact same issue. Commented May 13, 2015 at 23:49
  • if you need to recursively search thru the directories, you can use SPL libraries for that also stackoverflow.com/questions/20045622/… Commented May 13, 2015 at 23:57
  • 1
    @Baylock does ./images/ only contain images, or does it contain subfolders as well? do you need to look into its subfolders too for images? Commented May 14, 2015 at 0:09

1 Answer 1

4

Just to explain the part wherein it gives you the Array.

First off, scandir returns the following:

Returns an array of files and directories from the directory.

From that return values, it returned this (this is an example, for reference):

Array
(
    [0] => . // current directory
    [1] => .. // parent directory
    [2] => imgo.jpg
    [3] => logo.png
    [4] => picture1.png
    [5] => picture2.png
    [6] => picture3.png
    [7] => picture4.png
)

Those dots right there are actually folders. Right now in your code logic, when it hits/iterate this spot:

if(is_dir($x))$ar[$x]=scandir($x); // if its a directory
// invoke another set of scandir into this directory, then append it into the array

Thats why your resultant array has mixed strings, and that another extra/unneeded scandir array return values from ..

A dirty quick fix could be used in order to avoid those. Just skip the dots:

foreach($g as $x)
{
    // skip the dots
    if(in_array($x, array('..', '.'))) continue;
    if(is_dir($x))$ar[$x]=scandir($x);
    else
    {
        $count++;
        $ar[]=$x;
    }
}

Another alternative is to use DirectoryIterator:

$path = './images/';
$files = new DirectoryIterator($path);
$ar = array();
foreach($files as $file) {
    if(!$file->isDot()) {
        // if its not a directory
        $ar[] = $file->getFilename();
    }
}

echo '<pre>', print_r($ar, 1);
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you very much. That's it. The other contributors are probably right with their suggestions but I needed to know what happened here and your answer makes total sens.
@Baylock sure glad this shed some light
@l'L'l yes of course glob would be cool and flexible since it'll support even with wildcards and other stuff, sure no harm done.
@Ghost: Here's what I was thinking - feel free to add it to your answer if you think it's helpful, cheers :)
Don't kid yourself; give yourself more credit. I thought you gave them a rather good explanation ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.