Total noob in PHP need help with creating new folders

Discussion in 'PHP' started by TheSyndicate, Jun 26, 2008.

  1. #1
    Total noob in PHP need help with creating new folders

    I want to create a new page for every new “username”

    Mydomain.com/user1

    Mydomain.com/user2

    I also put in the username on the page but that is ok but what I do not know is how do create new pages automatically.

    Creating 1000 folders will take some time :rolleyes:
     
    TheSyndicate, Jun 26, 2008 IP
    itnashvilleCOM likes this.
  2. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #2
    If you have your users stored on a mysql database, you could use a for loop to parse al the data on the mysql table and create a new folder with each user. For example:

    <?php
    
    mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    
    $query_users = "SELECT * FROM users";
    $result_users = mysql_query($query_users);
    $num_result_users = mysql_num_rows($result_users);
    
     for ($i_users=0; $i_users <$num_result_users; $i_users_id++)  {
        $row_users = mysql_fetch_array($result_users);   
        mkdir($row_users['user'], 0755);
      }
    sleep(1);
    }
    ?>
    PHP:
    This example code will parse all the entries of the mysql users table and create a folder with the name of the user field and 0755 permissions.
     
    dannet, Jun 26, 2008 IP
    TheSyndicate likes this.
  3. TheSyndicate

    TheSyndicate Prominent Member

    Messages:
    5,410
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    365
    #3
    Thanks a bunch.

    Is there a way i can change the pages inside the folders as well?

    I am trying figure out if i should use pages, subdomains or folders
     
    TheSyndicate, Jun 26, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Hmm, have you not thought of using mod_rewrite to point all the folders to a main handling script?

    /user/[id] --> /user.php?id=[id]

    This allows you to only edit one file, to do everything...

    Dan
     
    Danltn, Jun 27, 2008 IP
  5. TheSyndicate

    TheSyndicate Prominent Member

    Messages:
    5,410
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    365
    #5
    You mean just have one file and change the username on each page with PHP? yes thats what i will do.
     
    TheSyndicate, Jun 27, 2008 IP
  6. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why do you have 2 curly brackets and what is the sleep() for?
     
    Riverofrhyme, Jun 27, 2008 IP
  7. TheSyndicate

    TheSyndicate Prominent Member

    Messages:
    5,410
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    365
    #7
    I guess he want it to stop or pause but as i said i am a noob
     
    TheSyndicate, Jun 27, 2008 IP
  8. qprojects

    qprojects Peon

    Messages:
    1,901
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think the 2 braces is a typo.
    sleep() is to pause the execution of the script for 1 sec.
     
    qprojects, Jun 27, 2008 IP
  9. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Sorry, I wasn't clear. I know what the sleep does, but why was it used?
     
    Riverofrhyme, Jun 27, 2008 IP
  10. qprojects

    qprojects Peon

    Messages:
    1,901
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    0
    #10
    If it was inside foreach, most probably to execute the script slowly. :|
    Lets wait for dannet to respond though.
     
    qprojects, Jun 27, 2008 IP
  11. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #11
    Sorry was my error, it should be:

     for ($i_users=0; $i_users <$num_result_users; $i_users_id++)  {
        $row_users = mysql_fetch_array($result_users);   
        mkdir($row_users['user'], 0755);
        sleep(1);
      }
    
    PHP:
    The sleep is to wait after every directory creation and not to charge the server creating the 1000 folders at same time, its optional of course.
     
    dannet, Jun 27, 2008 IP
    itnashvilleCOM likes this.
  12. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Try this:


    
    <?php
    
    $R1 = mysql_query('SELECT `username` FROM `users`') or die(mysql_error());
    if (mysql_num_rows($R1) == 0) {
         die('Nothing to do.');
    }
    while ($r = mysql_fetch_array($R1)) {
         $uname = stripslashes($r['username']);
         $uname = urlencode($uname);
         mkdir($uname, 0755);
    }
    die('All done.');
    ?>
    
    PHP:
    That will clean the username up to be safe for a directory name.
     
    itnashvilleCOM, Jun 28, 2008 IP
  13. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #13
    Yes, there is an extra bracket, it was a typing error, and the sleep should be in the loop.

     
    dannet, Jun 28, 2008 IP
  14. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #14
    lol, it's all good.
     
    itnashvilleCOM, Jun 28, 2008 IP
  15. TheSyndicate

    TheSyndicate Prominent Member

    Messages:
    5,410
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    365
    #15
    So what is the correct code now?
     
    TheSyndicate, Jun 28, 2008 IP