Create a folder in php?

Discussion in 'PHP' started by sowers, Sep 12, 2008.

  1. #1
    Hello,

    I was wondering if this could be done. Say I have a url

    http://www.thewebsitedomnamehere.com/file.php?id=

    then the id value could be any value 1, 2, 3, 4 etc...

    Is there anyway I could automatically create a folder (named with that id value) with 777 chmod permissions when the page loads, and if the folder already exists then do nothing?

    ie a folder named '1' for example

    Thanks
     
    sowers, Sep 12, 2008 IP
  2. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    allaboutgeo, Sep 12, 2008 IP
  3. nabz245

    nabz245 Well-Known Member

    Messages:
    677
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #3
    <?php
    mkdir($_GET['id'], 0777);
    ?>
    
    Code (markup):
    If you want to only allow numbers to create a dir use:
    <?php
    if (is_numeric($_GET['id'])) mkdir($_GET[id], 0777);
    ?>
    
    Code (markup):
    I hope that helps!
     
    nabz245, Sep 12, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Use chmod("filename",077) to set folder permission once folder is created.
    regards :)
     
    JEET, Sep 12, 2008 IP
  5. nabz245

    nabz245 Well-Known Member

    Messages:
    677
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #5
    
    <?php
    mkdir($_GET['id'], 0777);
    ?>
    
    Code (markup):
    Should already set the permissions. Also, it should be 0777 not 077:)
     
    nabz245, Sep 13, 2008 IP
  6. Gmorkster

    Gmorkster Peon

    Messages:
    202
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Don`t forget to run sanity checks on $_GET['id'], you don`t want to end up trying to create a folder called /etc/passwd or something :)
     
    Gmorkster, Sep 13, 2008 IP