douduan5753 2011-10-19 08:23
浏览 31
已采纳

指示PHP忽略扫描文件夹

I've been working with a handy php script, which scans my site and spits out links to all the pages it finds. The problem is, it's also scanning my 'includes' folder, which contains all my .inc.php files.

Obviously I'd rather it ignore this folder when scanning the site, but for the life of me I can't see how to edit the script to tell it to do so.

The script is:

<?php 
// starting directory. Dot means current directory
$basedir = ".";

// function to count depth of directory 
function getdepth($fn){
return (($p = strpos($fn, "/")) === false) ? 0 : (1 + getdepth(substr($fn, $p+1)));
}

// function to print a line of html for the indented hyperlink
function printlink($fn){
  $indent = getdepth($fn); // get indent value
  echo "<li class=\"$indent\"><a href=\"$fn\">"; //print url
$handle = fopen($fn, "r"); //open web page file
$filestr = fread($handle, 1024); //read top part of html
fclose($handle); //clos web page file
if (preg_match("/<title>.+<\/title>/i",$filestr,$title)) { //get page title
    echo substr($title[0], 7, strpos($title[0], '/')-8); //print title
} else {
    echo "No title";
}
  echo "</a></li><br>
"; //finish html
}

// main function that scans the directory tree for web pages 
function listdir($basedir){
if ($handle = @opendir($basedir)) { 
    while (false !== ($fn = readdir($handle))){ 
        if ($fn != '.' && $fn != '..'){ // ignore these
            $dir = $basedir."/".$fn; 
            if (is_dir($dir)){ 
                listdir($dir); // recursive call to this function
            } else { //only consider .html etc. files
                if (preg_match("/[^.\/].+\.(htm|html|php)$/",$dir,$fname)) {
                   printlink($fname[0]); //generate the html code
                }
                            } 
        } 
    } 
    closedir($handle); 
    } 
} 
// function call 
listdir($basedir); //this line starts the ball rolling
?>

Now I can see where the script is told to ignore certain files, and I've tried appending:

&& $dir != 'includes'

...to it in numerous places, but my php knowledge is simply too shaky to know exactly how to integrate that code into the script.

If anyone can help, then you'd be saving me an awfully large headache. Cheers.

  • 写回答

2条回答 默认 最新

  • dougu4027 2011-10-19 08:30
    关注

    Add it this line:

    if ($fn != '.' && $fn != '..'){ // ignore these
    

    so it's

    if ($fn != '.' && $fn != '..' && $fn != 'includes'){ // ignore these
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大