codeigniter在视图中获取目录信息

时间:2012-06-08 10:45:08

标签: php codeigniter

我基本上很难将动态变量传递给视图。

我有php功能如下:     

public function scandir_recursive($directory, $filter=FALSE)
{
 // if the path has a slash at the end we remove it here
 if(substr($directory,-1) == '/')
 {
     $directory = substr($directory,0,-1);
 }

 // if the path is not valid or is not a directory ...
 if(!file_exists($directory) || !is_dir($directory))
 {
     // ... we return false and exit the function
     return FALSE;

 // ... else if the path is readable
 }elseif(is_readable($directory))
 {
     // initialize directory tree variable
     $directory_tree = array();

     // we open the directory
     $directory_list = opendir($directory);

     // and scan through the items inside
     while (FALSE !== ($file = readdir($directory_list)))
     {
         // if the filepointer is not the current directory
         // or the parent directory
         if($file != '.' && $file != '..')
         {
             // we build the new path to scan
             $path = $directory.'/'.$file;

             // if the path is readable
             if(is_readable($path))
             {
                 // we split the new path by directories
                 $subdirectories = explode('/',$path);

                 // if the new path is a directory
                 if(is_dir($path))
                 {
                     // add the directory details to the file list
                     $directory_tree[] = array(
                         'path'    => $path,
                         'name'    => end($subdirectories),
                         'kind'    => 'directory',

                         // we scan the new path by calling this function
                         'content' => scandir_recursive($path, $filter));

                 // if the new path is a file
                 }elseif(is_file($path))
                 {
                     // get the file extension by taking everything after the last dot
                     $extension = end(explode('.',end($subdirectories)));

                     // if there is no filter set or the filter is set and matches
                     if($filter === FALSE || $filter == $extension)
                     {
                         // add the file details to the file list
                         $directory_tree[] = array(
                             'path'      => $path,
                             'name'      => end($subdirectories),
                             'extension' => $extension,
                             'size'      => filesize($path),
                             'kind'      => 'file');
                     }
                 }
             }
         }
     }
     // close the directory
     closedir($directory_list);

     // return file list
     return $directory_tree;

// if the path is not readable ...
 }else{
     // ... we return false
     return FALSE;
    }
}
    </code>

我需要使这样的功能可操作,我可以递归地回显从目录中检索的数据,并将这些数据传递给视图。

1 个答案:

答案 0 :(得分:2)

在控制器中执行以下操作:

$data['directory_tree'] = scandir_recursive($directory);
$this->load->view('my_view', $data);

在视图中,您可以像这样访问变量:

echo $directory_tree['path'];