简体   繁体   English

无法从 scandir 数组中删除“../”..

[英]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 "."我正在使用 scandir 将一些文件名放入数组中,然后尝试使用 if stmt 对“。”进行切片。 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:数组内容: 数组内容

Use a for loop starting at line 2 to skip the .使用从第 2 行开始的for循环来跳过. / .. . / ..

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

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() .另一种快速方法是使用array_diff()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM