script that makes directory on server

Discussion in 'PHP' started by tisucu, Apr 22, 2010.

  1. #1
    I need to make php script that makes directory named by variable on the server
     
    tisucu, Apr 22, 2010 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    <?php
    $var = "folderName";
    mkdir($var, 0777);
    ?>
    PHP:
     
    Last edited: Apr 22, 2010
    crivion, Apr 22, 2010 IP
  3. tisucu

    tisucu Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that easy, great thanks

    is there easier way to do this, what troubles me if I make folders like this new1/new2/ if new1 dont exists i get error, I can branch only one folder not folder in folder in folder

    <?php
    $var1="sites";
    $var2="files";
    $var3="data";
    $var4="test";
    
    $v1=$var1;
    $v2=$var1."/".$var2;
    $v3=$var1."/".$var2."/".$var3;
    $v4=$var1."/".$var2."/".$var3."/".$var4;
    
    if (file_exists($v4)) { echo "Exists!";
    }
    elseif (file_exists($v3)) {
    mkdir($v4, 0755);
    }
    elseif (file_exists($v2)) { 
    mkdir($v3, 0755);
    mkdir($v4, 0755);
    }
    elseif (file_exists($v1)) {
    mkdir($v2, 0755);
    mkdir($v3, 0755);
    mkdir($v4, 0755);
    }
    else { 
    mkdir($v1, 0755);
    mkdir($v2, 0755);
    mkdir($v3, 0755);
    mkdir($v4, 0755);
    }
    ?>
    PHP:
     
    Last edited: Apr 22, 2010
    tisucu, Apr 22, 2010 IP
  4. System 6 Hosting

    System 6 Hosting Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Leaving off the if exists test...

    <?php
    $directory=array('sites','files','data','test');
    $v='';
    foreach($directory as $var)
    {
      $v.=$var;
      mkdir($v,0755);
    }
    ?>
    PHP:
     
    Last edited: Apr 23, 2010
    System 6 Hosting, Apr 23, 2010 IP
  5. tisucu

    tisucu Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Great! I was looking today at foreach but I didn't understud what exactly would be $var so I gave up
    This is exactly what I was looking for, I can change arrays to make new1/new2/new3/new4 with / or make new folder branch without /
    Thanks

    <?php
    $directory=array('sites','/files','/data','/test');
    $v='';
    foreach($directory as $var)
    {
    $v.=$var;
    if (file_exists($v)) {}
    else 
     {
     mkdir($v,0755);
     }
    }
    ?>
    PHP:
     
    Last edited: Apr 23, 2010
    tisucu, Apr 23, 2010 IP