0

私は便利な php スクリプトを使って作業してきました。このスクリプトはサイトをスキャンし、見つかったすべてのページへのリンクを吐き出します。問題は、すべての .inc.php ファイルを含む「includes」フォルダーもスキャンしていることです。

明らかに、サイトをスキャンするときにこのフォルダーを無視したいのですが、私の人生では、スクリプトを編集してそうするように指示する方法がわかりません.

スクリプトは次のとおりです。

<?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>\n"; //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
?>

これで、スクリプトが特定のファイルを無視するように指示されている場所を確認でき、追加しようとしました:

&& $dir != 'includes'

...多くの場所でそれを行っていますが、私のphpの知識は、そのコードをスクリプトに統合する方法を正確に知るにはあまりにも不安定です.

誰かが助けてくれるなら、あなたは私を非常に大きな頭痛の種から救ってくれるでしょう. 乾杯。

4

2 に答える 2

1

次の行を追加します。

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

っていうことは

if ($fn != '.' && $fn != '..' && $fn != 'includes'){ // ignore these
于 2011-10-19T08:30:00.723 に答える
1

あなたの道は絶対的である必要があります。listdirの先頭に追加します

function listdir($basedir){
    if($basedir == '/path/to/includes') {
         return;
    } [...]

これにより、1つのインクルードフォルダーのみが無視されるようになります。

于 2011-10-19T08:31:22.390 に答える