SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create folders in 1 run

    Can we create 3 folders in one run as in following fashion?
    1st step: Create folder at root (say folder_1)
    2nd step: create folder inside folder_1 (say folder_2)
    3rd step: create folder inside folder_2 (say folder_3)

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, but it would mean granting chmod 777 permissions to the root folder, and I don't know if web hosts would allow that.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why the need for 777 permissions?

    PHP Code:
    mkdir('folder_1');
    mkdir('folder_1/folder_2');
    mkdir('folder_1/folder_2/folder_3'); 

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does the root folder not need appropriate permissions (at the filesystem level) for the mkdir() function to work? (Maybe not - a look at the manual entry for mkdir() shows the following ...
    Quote Originally Posted by PHP Manual Comments
    bat at flurf dot net (22-Mar-2006 05:23)

    A little understanding of LISPlike programming makes this sort of task (recursive mkdir()ing in PHP4) much simpler. This version works nicely:
    PHP Code:
    <?php
    function MakeDirectory($dir$mode 0755)
    {
        if (
    is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
        if (!
    MakeDirectory(dirname($dir),$mode)) return FALSE;
        return @
    mkdir($dir,$mode);
    }
    ?>
    How it works: line one attempts to make the directory, and returns TRUE if it works or if it already exists. That's the easy case if the parent directories all exist.
    Line two trims off the last directory name using dirname(), and calls MakeDirectory recursively on that shorter directory. If that fails, it returns FALSE, but otherwise we come out of it knowing that the parent directory definitely exists.
    Finally, presuming the recursive call worked, once we get to line three we can create the requested directory.
    Note the use of @ to suppress warning messages from mkdir.
    The beauty of this is that if, say, the great-grandparent directory exists but the grandparent and parent directories don't, the function will simply call itself recursively until it gets high enough up the tree to do some work, then carry on unwinding back until all the new directories have been created.
    This is pretty bog-standard recursive programming. Anyone who can't wrap their head around it after a few minutes of concentration should probably try a career in sales.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "-p" flag in *nix for mkdir - you'll need to execute a shell command.

    PHP Code:
    $path "$folder_1/$folder_2/$folder_3";
    `
    mkdir -p $path`; 

  6. #6
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes am also looking for a code like above.

    $path = "$folder_1/$folder_2/$folder_3";
    `mkdir -p $path`;
    this code can be possible rather than shell command ?

  7. #7
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is also useful for me

    <?php
    function MakeDirectory($dir, $mode = 0755)
    {
    if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
    if (!MakeDirectory(dirname($dir),$mode)) return FALSE;
    return @mkdir($dir,$mode);
    }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •