2011-09-08 235 views
0

我似乎无法使粗体文件路径工作。我希望脚本扫描Dir_3。任何帮助将非常感激。如何用PHP opendir读取当前目录之外的目录?

<?php 

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

// These file extensions will be ignored 
$excludedExtensions = array (
    'html', 
'xslt', 
'htm', 
'ahk', 
'xsl', 
'txt', 
'xml' 
); 

// 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('**http://www.mydomain.com/Dir_1/Dir_2/Dir_3/**')) { 
    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); 
} 

?> 

回答

4

您无法'打开'一个URL。但是你可以在执行opendir指定相对和绝对路径:

opendir('.') // open current directory 
opendir('..') // open parent directory 
opendir('childdir') // open the 'childdir' directory which exists in the current directory (.) 
opendir('childdir/grandchilddir') // open a grandchild directory down two levels from the current dir 
opendir('../brotherdir') // open a directory next to the current one 
opendir('../brotherdir/nephewdir') // open a director next to and down one 
opendir('../..') // open a grandparent directory 
opendir('../../uncle') // yada yada yada 

opendir('/') // open the root dir (primordial slime/adam, depending on your viewpoint) 
opendir('/cain') // open a dir called "cain" which is housed in the root directory. 
+0

伟大的帮助了很多。非常感谢你。现在唯一的问题是它在页面上吐出的链接没有正确链接。我是否必须在其他地方调整路径才能正确连接? – mikenichols

+0

那么,基于Web的路径可能与文件系统上的路径完全不同。 opendir()/ fopen()/ etc的作用可能与这些目录在网站中的映射方式完全没有关系。 –

1

opendir()仅支持适当的文件系统路径,而不是http://路径。

+0

如果你有机会你可以使用[FTP]其他网站(http://php.net/manual/en/ref.ftp.php) – Xeoncross