ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

get names of directories and place in a dropdown list

May 29, 2009 4:36am

Subscribe [4]
  • #1 / May 29, 2009 4:36am

    kathleenr

    1 posts

    I need to get all the folder names in a directory and place it in a dropdown list.

    Can you suggest a method on how to get the names because I don’t have the slightest idea to do this in PHP/CI.

    Thanks in advance.

  • #2 / May 29, 2009 4:50am

    kathleenr

    1 posts

    this was only simple :p

    scandir

  • #3 / May 29, 2009 5:30am

    eoinmcg

    311 posts

    kathleenr,


    I do it like this:

    $this->load->helper('file');
                $path = APPPATH.'/views/snippets/';
                $snippets = get_filenames($path);
                $data['html'] .= cms_multi_select('Snippets', 'snippets', $snippets, $row->snippets);

    that loads all files from the snippets folder, which is inside the views folder. Then the values are passed to a helper function (which of course you’ll need to create then load)

    function cms_multi_select($label, $name, $options, $val='')
        {
    
            $val = explode(',', $val);
    
            $form = "\n\t".'<div>'.$label.'';
            foreach($options as $option)
            {
    
                $option = str_replace('.php', '', $option);
                $selected = '';
    
                if(is_array($val))
                {
                    if(in_array($option, $val))
                        $selected = ' checked';
                }
    
                $form .= '<div class="row"><input type="checkbox" name="'.$name.'[]" value="'.$option.'"'.$selected.'>'.ucfirst($option).'</div>';
            }
    
            $form .= "\n\t".'</div>';
    
            return $form;
    
        }

    and that should do it

  • #4 / May 29, 2009 8:38am

    NetStranger

    12 posts

    i have the question!
    if i want to get name of folder in my directory , but folder name is “папка” ? Name of the folder on cyrillic ?  this function get name and print only “?????”! plz help!

  • #5 / May 29, 2009 9:10am

    eoinmcg

    311 posts

    NetStranger,

    What operating system are using?

    I just tried what you described with a cyrillic folder name and it worked fine for me on Linux.

    Not sure if your problem could be related to the version of windows you’re using… if so you may just have to rename the folder to have only latin characters

  • #6 / May 29, 2009 9:40am

    NetStranger

    12 posts

    i modernizate default directory_helper :

    if ( ! function_exists('directory_map'))
    {
        function directory_map($source_dir, $top_level_only = FALSE)
        {
           
            if ($fp = @opendir($source_dir))
            {
                $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;        
                $filedata = array();
                
                while (FALSE !== ($file = readdir($fp)))
                {
                    if (strncmp($file, '.', 1) == 0)
                    {
                        continue;
                    }
                    
                    if ($top_level_only == FALSE && @is_dir($source_dir.$file))
                    {
                        $temp_array = array();
                    
                        $temp_array = directory_map($source_dir.$file.DIRECTORY_SEPARATOR);
                    $file = iconv("windows-1251", "UTF-8", $file);  // HERE !!!
                        $filedata[$file] = $temp_array;
                    }
                    else
                    {
                        $filedata[] = $file;
                    }
                }
                
                closedir($fp);
                return $filedata;
            }
        }
    }

    now it takes me right names of the folders , BUT if i want to get list of files in this cyrillic folder , i get an error! i think my problem is $source_dir ... but i dont know how to solve the problem (((

  • #7 / May 29, 2009 9:50am

    eoinmcg

    311 posts

    Hmmmm….

    I don’t have a windows box handy to try it out.

    What is the error message you get?

  • #8 / May 29, 2009 9:59am

    NetStranger

    12 posts

    this error:

    Severity: Warning
    
    Message: Invalid argument supplied for foreach()
    
    Filename: controllers/photo.php
    
    Line Number: 38

    this it controller Photo

    <........>
    35. 
    36. $data = directory_map('./img/photo/папка');
    37.
    38.   foreach ($data as $val => $img) 
    39.                 {
    <.......>

    when folder name is latin - it’s ok

  • #9 / May 29, 2009 10:13am

    Evil Wizard

    223 posts

    The function directory_map() returns NULL if the directory does not exist, otherwise it returns an array of the directory entries. try

    <........>
    35.
    36. $data = directory_map('./img/photo/папка');
    37.  if(!is_null($data)) {
    38.      foreach ($data as $val => $img) {
    39.      
    <.......>
  • #10 / May 29, 2009 10:16am

    NetStranger

    12 posts

    but my directory exists! )) i need to scan it)

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases