TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-02-2008, 08:05 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Application Go Creating directory hierarchies with ease

I created this function today because we desperately needed a solution to save on repeated code. Such as checking if the directory exists, and if not creating it. Then looping through again and seeing if that exists, etcetera, etcetera.

The function I came up with accepts a directory path. For example: C:\wamp\www\images\TalkPHP\screenshots\. It will then loop through all the directories, ignoring things like C: and D: on Windows. If a particular directory doesn't exist then it will create it for you. If its parent is not writeable, then it will throw an exception telling you so.

I'm not overly keen on the subsequent exception, and you may also need to prepend @ in front of mkdir to suppress nasty PHP errors.

I am more than open to suggestions for improving this function, and that's half the reason I'm releasing it to the public. The other half is because it's a truly useful function! By all means, use it wherever you like!

The second argument for the function (Default: DIRECTORY_SEPARATOR) is a native PHP constant for detecting the backslash type depending on the operating system. This, of course, can be modified by specifying the second argument upon calling the function.

Without further ado:

php Code:
class Explorer
{
    public static function makeFoldersFromFilepath($szFilepath, $szSeperator = DIRECTORY_SEPARATOR)
    {
        $aParts = explode($szSeperator, $szFilepath);
        $szLevels = null;
        $aLevels = array();
       
        foreach ($aParts as $szPart)
        {
            $szParentLevels = implode($szSeperator, $aLevels);
            $aLevels[] = $szPart;
            $szLevels = implode($szSeperator, $aLevels);
           
            if (preg_match('~:$~i', $szPart) || is_dir($szLevels))
            {
                continue;
            }
           
            if (!is_writeable($szParentLevels))
            {
                throw new Exception('Cannot create folder "' . $szLevels . '" because its parent is not writeable');
            }
           
            if (!mkdir($szLevels))
            {
                throw new Exception('Cannot create folder "' . $szLevels . '" for an unknown reason');
            }
        }
    }
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
zxt3st (12-02-2008)
Old 12-02-2008, 11:09 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

In PHP 5 mkdir has the third argument of (bool) recursive (default FALSE). Couple that with the ability to specify the mode and to apply a context (neither of which you class can do) I'm not seeing any advantage of your class over calling the mkdir function normally.

That said, maybe I'm just entirely missing the point.
Salathe is offline  
Reply With Quote
Old 12-02-2008, 11:36 PM   #3 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Nice share wildhoney.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 12-04-2008, 02:28 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Thank you. Although as Salathe pointed out (smug git! ) the PHP manual should always be read first. I simply assumed that they'd not yet added the recursive mode, but it would appear they added it in PHP 5.0.0, and fixed the last bug with version 5.2.0.

Nonetheless, I still prefer the throwing of exceptions if PHP is unable to create the directory. That probably doesn't necessitate a rewrite, however.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual Directory Support? delayedinsanity General 4 11-08-2012 09:04 PM
[How To] Allow / Disallow Directory Browsing dynaweb Tips & Tricks 6 04-14-2009 12:57 PM


All times are GMT. The time now is 09:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design