Thatguy553
Thatguy553

Reputation: 57

Cannot remove "."/".." from scandir array

I was using scandir to get some filenames into an array and then trying to use an if stmt to slice the "." and ".." from the array if they existed, but that did not work so I tried to only echo the image if the variable was not ".." or "." and that did not work. Example Code Below:

if ($pages[$pageIndex] == "." || $pages[$pageIndex] == "..") {
  array_slice($pages, 0, 2);
  print_r($pages);
}
# And

while ($i < $pagesLength) {
  if ($pages[$pageIndex] != "." || $pages[$pageIndex] != "..") {
    echo "<img src='series/series_" . rawurlencode($_GET['series']) . "/series_" . rawurlencode($newLink) . "/" . rawurlencode($pages[$pageIndex]) . "'>";
    $pageIndex = $pageIndex + 1;
    $i++;
  }
}                            }

They Stayed no matter what I did, How can I get rid of them?

Array Content: array contents

Upvotes: 1

Views: 73

Answers (2)

Cyclonecode
Cyclonecode

Reputation: 30071

A good way to strip out any hidden files would be:

$pages = preg_grep('/^(?!\.{1,2}$)/', $pages);

one other quick way would be to use array_diff().

$pages = array_diff($pages, array('.', '..'));

Upvotes: 0

Mech
Mech

Reputation: 4015

Use a for loop starting at line 2 to skip the . / ...

for ($i = 2; $i <= $pagesLength; $i++) {
  // do something
}

Upvotes: 1

Related Questions