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

时间:2019-07-15 19:36:06

标签: php

我有 6 个文件夹,但我只想扫描 4 。这是结构文件夹的布置。

主文件夹包含三个子文件夹(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和Provisioning_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

2 个答案:

答案 0 :(得分:0)

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");

答案 1 :(得分:0)

扫描已使用解决  substr-function在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.";
    }
}