Welcome Guest, Not a member yet? Register   Sign In
Getting Folders in A Directory
#1

[eluser]Matt Egan[/eluser]
I'm not sure this is possible with one of Codeigniter's built in File helper, but correct me if I'm wrong... If I could extend the file helper, that'd be cool too..

So, I'm coming along really really well on my blog platform, (Working name, Codepress), and I'm trying to implement a simple theming system, for now, I know for a fact that my html has plenty of handles for the blog to be styled a lot of different ways. So right now, I'm just interested in having a user created css file that can be loaded.

In my config table one of the entries is a theme name, for right now, I want that theme name to be the name of the folder that the .css file is stored in. This means the name of the theme is the same as the folder name. This means that I can load my theme simpily by...

Code:
@import url(<?php echo base_url(); ?>content/themes/<?php echo $this->Blogmodel->get_config('theme_folder', false); ?>/styles.css);

Ok, lets get down to what I want to do. Somehow, though php, I need to get the name of all the folders in my "themes" directory, so when I update the config table, I can just dump the name of the selected folder in the drop-down into the table.

I'm thinking it may just be easier just to have there be a text file in each theme folder storing theme data (name, etc.). I think this would allow names with spaces and allow for other things to be displayed about the theme. Thoughts?

Thanks for your help guys. Oh, btw, you can find a demo here. It was the product of time in between my classes as a high school freshmen (I'm only 14 Big Grin) and I went on to win the Georgia state tech fair, although, my competition wasn't that tough. If anyone wants to see the admin side, or get a download of the project, just PM me.
#2

[eluser]TheFuzzy0ne[/eluser]
For scanning the directory structure, if you're running PHP5, [url="http://uk3.php.net/manual/en/function.scandir.php"]scandir[/url] should be ideal. Just remember not to display the '.' and '..'.

I've had similar thoughts in the past with regards to placing a text file inside each of the directories. Perhaps we're onto something?
#3

[eluser]Matt Egan[/eluser]
Holy cow that a fast reply. Scandir, I'll take a look real quick. Thanks Big Grin

As with the text file, Ill probably write a quick class to grab the data about the theme from it. Sounds simple..
#4

[eluser]jedd[/eluser]
Hi Matt - site looks good.

Have you tried using the standard PHP opendir() / is_dir() functions?

Untested, but possibly something like:
Code:
$theme_dir = $base_url() ."/assets/themes/";

$themes = array ();

$td_handle = opendir($theme_dir);

while ($dir_entry = readdir ($td_handle))  {
    if (filetype ($dir_entry) == "dir")
        $themes[] = $dir_entry;
    }

Note that you'll get . and .. as directories - this has bitten me before, but it's easy to check for. Similarly you'd want some other error handling around there too, of course - confirming the themedir exists, and so on.
#5

[eluser]Matt Egan[/eluser]
Ok. using jedd's idea (Quite ingenious I must say), I created this function in my model
Code:
function theme_list(){
        
        $theme_dir = "content/themes/";
        $themes = array();

        $handle = opendir($theme_dir);

        while ($entry = readdir($handle))  {
            
            if (filetype($theme_dir.$entry) == "dir")    {
        
                $themes[] = $entry;
                
            }
        
        }
        
        return $themes;
        
    }

I was getting an error, and discovered that the filetype function needs the path to the file, so I simply just prepended the directory to the filename. Nifty stuff. Now I'm working on getting that nasty little . and .. outta there. Ill post back later if I don't have to hit they hay first.
#6

[eluser]TheFuzzy0ne[/eluser]
Code:
function theme_list(){
        
        $theme_dir = "content/themes/";
        $themes = array();

        $handle = opendir($theme_dir);

        while ($entry = readdir($handle))  {
            
            if (filetype($theme_dir.$entry) == "dir" &&
                $entry != '.' &&
                $entry != '..')    {
        
                $themes[] = $entry;
                
            }
        
        }
        
        return $themes;
        
    }

The above code is untested.
#7

[eluser]Matt Egan[/eluser]
Thanks for the help guys. Here's my final piece of code. I also needed the currently active theme to be selected already, so I just went ahead and put it first in the array, this is so that if the user submits the form without changing the theme drop-down, the theme does not change. If there's any way to call the model from the model itself, that'd be cool, but I'm don't think you can.

Code:
function theme_list(){
    
    $theme_dir = "content/themes/";
    
    $this->db->where('field', 'theme_folder');
    $query = $this->db->get('config');
    $row = $query->row();
    $active = $row->value;
    
    $themes = array();

    $themes[] = $active;

    $handle = opendir($theme_dir);

    while ($entry = readdir($handle))  {
        
        if (filetype($theme_dir.$entry) == "dir" && $entry !="." && $entry !=".." && $entry != $active)    {
    
            $themes[] = $entry;
            
        }
    
    }
    
    return $themes;
    
}
#8

[eluser]xwero[/eluser]
why not use glob
Code:
$parent = FCPATH.'/content/themes/';

$children = glob($parent.'*',GLOB_ONLYDIR);
// remove parent path
foreach($children as $i=>$child){ $children[$i] = str_replace($parent,'',$child); }
If you use your code close the handle.




Theme © iAndrew 2016 - Forum software by © MyBB