English 中文(简体)
查阅除目录外的所有档案
原标题:Read all files in directory except
  • 时间:2011-10-29 20:41:40
  •  标签:
  • php

以下职能从包括所有分局在内的特定名录中退回了各种XML文件。 我如何通过第二个排除名录的任择参数来修改这一职能。

E.g: 
getDirXmlFiles($config_dir, "example2");

Directory/Files
    /file1.xml
    /file2.xml
    /examples/file3.xml
    /examples/file4.xml
    /example2/file5.xml
    /example2/file5.xml

在上述情况下,该职能将归还所有档案,但例如2目录中的档案除外。

function getDirXmlFiles($base) {        

    $files = array();       
    if(!is_dir($base)) return $files;

    if ($handle = opendir($base)) {
        while (false !== ($file = readdir($handle))) {
            if ($file == "." || $file == "..") continue;

            if(is_dir("$base/$file")) {                 
                $subfiles = $this->getDirXmlFiles("$base/$file");
                $files = array_merge($files, $subfiles);                    
            } else {                    
                if(Cms_File::type($file,false) == "xml") 
                    $files[] = "$base/$file";
            }
        }
        closedir($handle);
    }
    return $files;
}
最佳回答

重新设计职能投入:

function getDirXmlFiles($base, $excludeDir) { 

然后改变这一思路:

if(is_dir("$base/$file")) { 

为此:

if(is_dir("$base/$file") && "$base/$file" != "$base/$excludeDir") { 
问题回答

为此:

function getDirXmlFiles($base, $exclude = NULL) {        

    $files = array();       
    if(!is_dir($base)) return $files;

    if ($handle = opendir($base)) {
        while (false !== ($file = readdir($handle))) {
            if ($file == "." || $file == ".." || "$base/$file" == $exclude) continue;

            if(is_dir("$base/$file")) {                 
                $subfiles = $this->getDirXmlFiles("$base/$file",$exclude);
                $files = array_merge($files, $subfiles);                    
            } else {                    
                if(Cms_File::type($file,false) == "xml") 
                    $files[] = "$base/$file";
            }
        }
        closedir($handle);
    }
    return $files;
}

You can just slightly modify the function to check and see if the current directory is the one you want to exclude or not

function getDirXmlFiles($base,$exclude) {        

    $files = array();       
    if(!is_dir($base)) return $files;

    if ($handle = opendir($base)) {
        while (false !== ($file = readdir($handle))) {
            if ($file == "." || $file == "..") continue;

            if(is_dir("$base/$file") && $file != $exclude) {                 
                $subfiles = $this->getDirXmlFiles("$base/$file",$exclude);
                $files = array_merge($files, $subfiles);                    
            } else {                    
                if(Cms_File::type($file,false) == "xml") 
                    $files[] = "$base/$file";
            }
        }
        closedir($handle);
    }
    return $files;
}

类型之长(仅测试了yn合物验证,可能需要一些小偷盗)(这将允许你排除多个目录名称......)

function getDirXmlFiles($base, $excludeArray = array()) {    

    if (!is_array($excludeArray))
        throw new Exception(__CLASS__ . "->" . __METHOD__ . " expects second argument to be a valid array");

    $files = array();       
    if(!is_dir($base)) return $files;

    if ($handle = opendir($base)) {
        while (false !== ($file = readdir($handle))) {

            $path = $base . "/" . $file;
            $isDir = is_dir($path);

            if (
                $file == "."
                || $file == ".."
                || (
                    $isDir
                    && count($excludeArray) > 0 
                    && in_array($file, $excludeArray)
                )
            ) 
                continue;

            if($isDir) {                 
                $subfiles = $this->getDirXmlFiles($path, $excludeArray);
                $files = array_merge($files, $subfiles);                    
            } else {                    
                if(Cms_File::type($file,false) == "xml") 
                    $files[] = $path;
            }
        }
        closedir($handle);
    }
    return $files;
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签