繁体   English   中英

如何使用PHP readdir读取根级别和链接之外的目录中的文件?

[英]How to use PHP readdir to read files in directories outside of the root level and link?

我正在使用驻留在Web服务器根目录下的php include(侧面导航菜单,即php脚本所在的位置)。

我想使用php readdir或scandir列出服务器(/ report_1)上其他文件夹的内容,并带有指向这些文件的链接,但不包括html,htm和xslt扩展名。 (或仅包括php扩展名)

目前,我使用的readdir仅读取其所在目录的内容并返回不包含某些文件类型的链接(请参见下面的代码)

我最终希望在包含的php文件中列出并链接到“包含”根级别之外的文件。 任何帮助将不胜感激。

<?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);
}

?>
  • 使目录循环起作用。
  • 使用is_dir检查当前文件是否为目录
  • 如果是这样,请通过相同的功能重新运行该目录。
  • 否则,在执行操作时打印文件名。

您可能要考虑在链接中使用绝对路径。

希望这可以帮助。

暂无
暂无

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

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