加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何使用PHP readdir排除某些文件类型?

发布时间:2020-12-13 21:43:10 所属栏目:PHP教程 来源:网络整理
导读:我正在使用一个php扫描目录脚本,它将扫描目录的内容,然后使用指向目录内容的链接填充页面. ?php$count = 0;if ($handle = opendir('.')) {while (false !== ($file = readdir($handle))) { if ($file != "." $file != "..") {$count++; print("a href="".$f
我正在使用一个php扫描目录脚本,它将扫描目录的内容,然后使用指向目录内容的链接填充页面.

<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {$count++;
        print("<a href="".$file."">".$file."</a><br />n");
    }
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?

我想知道如何排除某些文件或文件类型,如test.xml,durka.xslt或.html显示在填充页面上.我有一些代码,但不知道如何集成它.任何帮助将非常感谢…

?php
if ($handle = opendir(‘.’)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “..”
&& $file != “NOT-TO-BE-IN-1.php”
&& $file != “NOT-TO-BE-IN-2.html”
&& $file != “NOT-TO-BE-IN-3.jpg”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”

)

解决方法

<?php

  // These files will be ignored
  $excludedFiles = array (
    'excludeMe.file','excludeMeAs.well'
  );

  // These file extensions will be ignored
  $excludedExtensions = array (
    'html','htm','php'
  );

  // Make sure we ignore . and ..
  $excludedFiles = array_merge($excludedFiles,array('.','..')); 

  // Convert to lower case so we are not case-sensitive
  for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
  for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);

  // Loop through directory
  $count = 0;
  if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
      $extn = explode('.',$file);
      $extn = array_pop($extn);
      // Only echo links for files that don't match our rules
      if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)) {
        $count++;
        print("<a href="".$file."">".$file."</a><br />n");
      }
    }
    echo '<br /><br /><a href="..">Return</a>';
    closedir($handle);
  }

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读