如何避免通过 php 扫描某些目录

  • 本文关键字:扫描 php 何避免 php
  • 更新时间 :
  • 英文 :


我有 6 个文件夹,但我只想扫描 4 个。 这是结构文件夹排列。

main_folder包含三个子文件夹(second_folder、third_folder 和 Telex_folder(third_folder包含子文件夹(cosmetic_folder和provision_folder(等。

目前使用下面的代码,我可以扫描所有目录所有子目录中的所有文件,并且它工作得很好下面的代码。

function outputFiles1($path){
    // Check directory exists or not
    if(file_exists($path) && is_dir($path)){
        // Search the files in this directory
        $files = glob($path ."/*");
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                if(is_file("$file")){
                    // Display only filename
                    echo basename($file) ."<br>";
                } else if(is_dir("$file")){
                    // Recursively call the function if directories found
                    outputFiles1("$file");
                }
            }
        } else{
            echo "ERROR: No such file found in the directory.";
        }
    } else {
        echo "ERROR: The directory does not exist.";
    }
}
// Call the function
outputFiles1("C:/xampp/htdocs/first_main_folder");

现在我想扫描所有目录,同时保持cosmetic_folder和**provision_folder目录未扫描。

请问在扫描过程中如何避免扫描(cosmetic_folder和provision_folder(子目录。

谢谢

first_Main_folder
test.png
data.php
check.gif

    second_folder
    tony.jpg
    mark.gif
    action.php
    third_folder
    index.php
    post.php
    sender.php
    han.gif
        cosmetic_folder
            cosmetic1.csv
            cosmetic2.csv
        provision_folder
            prov1.xml
            prov2.pdf
    telex_folder
    contact1.csv
    conctact.pdf
function outputFiles1($path, $depth= 0){
    if ($depth > 1) {
        break;
        //or do what you want then ;)
    }
    // Check directory exists or not
    if(file_exists($path) && is_dir($path)){
        // Search the files in this directory
        $files = glob($path ."/*");
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                if(is_file("$file")){
                    // Display only filename
                    echo basename($file) ."<br>";
                } else if(is_dir("$file")){
                    // Recursively call the function if directories found
                    outputFiles1("$file", $depth+1);
                }
            }
        } else{
            echo "ERROR: No such file found in the directory.";
        }
    } else {
        echo "ERROR: The directory does not exist.";
    }
}
// Call the function
outputFiles1("C:/xampp/htdocs/first_main_folder");

扫描已使用 在 if 语句中传递的子函数。

  if( ($file != '*') && (substr( $file , -6 ) != 'cosmetic_folder') && (substr( $file , -6 ) != 'provision_folder') ){

//foreach continue.....
}

因此按照下面的代码解决

function outputFiles1($path){
    // Check directory exists or not
    if(file_exists($path) && is_dir($path)){
        // Search the files in this directory
        $files = glob($path ."/*");
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
  if( ($file != '*') && (substr( $file , -6 ) != 'cosmetic_folder') && (substr( $file , -6 ) != 'provision_folder') ){
                if(is_file("$file")){
                    // Display only filename
                    echo "$file"  . "<br>";
                } else if(is_dir("$file")){
                    // Recursively call the function if directories found
                    outputFiles1("$file");
                }
}
            }
        } else{
            echo "ERROR: No such file found in the directory.";
        }
    } else {
        echo "ERROR: The directory does not exist.";
    }
}

最新更新