-1

我想知道如何创建一个声明:

如果一个文件夹中存在两次名为 Setup.php 的文件和/或其关联的子文件夹,则返回一条消息。如果扩展名为 .css 的文件在文件夹或其任何子文件夹中多次存在,则返回一条消息

由于子文件夹,此函数必须是递归的。并且可以硬编码“Setup.php”或“.css”,因为它们是唯一需要寻找的东西。

我目前拥有的有点混乱,但确实有用(在我弄清楚这个问题后会进行重构)

protected function _get_files($folder_name, $type){
    $actual_dir_to_use = array();
    $array_of_files[] = null;
    $temp_array = null;
    $path_info[] = null;

    $array_of_folders = array_filter(glob(CUSTOM . '/' .$folder_name. '/*'), 'is_dir');
    foreach($array_of_folders as $folders){
        $array_of_files = $this->_fileHandling->dir_tree($folders);
        if(isset($array_of_files) && !empty($array_of_files)){
            foreach($array_of_files as $files){
                $path_info = pathinfo($files);
                if($type == 'css'){
                    if($path_info['extension'] == 'css'){
                        $actual_dir_to_use[] = $folders;
                    }
                }

                if($type == 'php'){
                    if($path_info['filename'] == 'Setup' && $path_info['extension'] == 'php'){
                        $temp_array[] = $folders;
                        $actual_dir_to_use[] = $folders;
                    }
                }
            }
        }

        $array_of_files = array();
        $path_info = array();
    }

    return $actual_dir_to_use;      
}

如果你传入说,packagesphppath/to/apples, path/to/bananas, path/to/fruit, path/to/cat, path/to/dog到函数中,我将查看 packages 文件夹并返回所有包含 Setup 且扩展名为 php的子文件夹名称(例如:)。

问题是如果 apples/ 包含多个 Setup.php,那么我会得到:path/to/apples, path/to/apples, path/to/bananas, path/to/fruit, path/to/cat, path/to/dog

所以我需要修改这个函数,或者单独写一个,满足上面的 sudo 代码。

问题?我不知道从哪里开始。所以我在这里寻求帮助。

4

2 回答 2

0

您可以在此处找到该类ipDirLiterator-删除除运行删除代码的文件之外的所有文件
我希望你明白了。

<?php
  $directory  = dirname( __FILE__ )."/test/";
  $actual_dir_to_use  = array();
  $to_find  = "php";

  $literator  = new ipDirLiterator( $directory, array( "file" => "file_literator", "dir" => "dir_literator" ) );
  $literator->literate();

  function file_literator( $file ) {
    global $actual_dir_to_use, $to_find;
    // use print_r( $file ) to see what all are inside $file

    $filename = $file["filename"]; // the file name
    $filepath = $file["pathname"]; // absolute path to file
    $folder   = $file["path"]; // the folder where the current file contains
    $extens   = strtolower( $file["extension"] );

    if ( $to_find === "php" && $filename === "Setup.php" ) {
      $actual_dir_to_use[]  = $folder;
    }
    if ( $to_find === "css" && $extens === "css" ) {
      $actual_dir_to_use[]  = $folder;
    }
  }

  function dir_literator( $file ) {}

  print_r( $actual_dir_to_use );

  // or check
  if ( count( $actual_dir_to_use ) > 1 ) {
    // here multiple files
  }
?>
于 2013-07-05T16:17:18.873 回答
-1

问:这是家庭作业吗?

假设“否”,则:

1)不,函数不需要递归

2) 在 Linux 下,你可以找到这样的匹配文件:find /somefolder -name somefile -print

3) 同样,您可以检测匹配是否在路径中出现零次、一次或多次,如下所示:

find /somefolder -name somefile -print|wc -l

于 2013-07-05T15:53:41.550 回答