2011-09-01 82 views
0

我正在使用php驻留在我的web服务器的根级别的include(侧导航菜单,其中php脚本将是)。如何使用PHP readdir读取根目录之外的目录中的文件并链接?

我想使用php readdir或scandir列出服务器上不同文件夹的内容(/ report_1),并指向不包含html,htm和xslt扩展名的文件的链接。 (或者仅仅只包含PHP扩展)

目前我使用的是READDIR只读取它驻留在当前目录中的内容,并返回排除某些类型的文件的链接。(见下面的代码)

我会最终想要包含一个php文件,列出并链接到“include”根级别之外的文件。任何帮助将非常感激。

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

?> 
+0

那么,你的问题是什么?你卡在哪里?你可以用'..'来访问父目录 - 你在找什么? –

+0

php代码将包含在包含include之外的其他目录中的文件中。 – mikenichols

回答

1
  • 对该目录循环的功能。
  • 检查当前文件是否是使用is_dir的目录
  • 如果是,则通过相同的函数重新运行该目录。
  • 否则按照您的操作打印文件名。

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

希望这会有所帮助。

相关问题