Urgent Help Needed - Using mysql DB to create files/folders

Discussion in 'PHP' started by amelen, Sep 27, 2008.

  1. #1
    We just had severe hardware failure and need urgent help.

    We have a mysql database of around 500,000 "usernames". We need to use that database to re-create folders on a server, and then upload a single file into all the folders.

    For example:

    Username
    demo
    demo2
    demo4
    test

    Server Folder Path (to be created)
    /home/d/e/demo
    /home/d/e/demo2
    /home/t/e/test


    So I need the server to automatically create all those dir's based on the username's in the database. Step two is to upload a single file into all of those directories (so the file would go into /home/d/e/demo, but not /home/d/ or /home/d/e/ .. needs to go into the account folders).

    Any advice?? I've been playing around with it for hours without any luck and we need it to get our free hosting operation back up after massive hardware failures.

    Thanks!! Any help will be GREATLY appreciated!!
     
    amelen, Sep 27, 2008 IP
  2. piehead2000

    piehead2000 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do something like...

    SELECT SUBSTRING(`username` FROM 1, 1) AS `first_letter`, SUBSTRING(`username` FROM 2, 1) AS `second_letter`, `username` FROM `users`

    Then loop through the results, use file_exists function to check if the directory & subdirectory has been made before. If not make it then fwrite a index.html file.

    Alternatively make an array of A to Z and loop through and make a directory & a subdirectory for each letter, you'll end up with 676 directories though. Once done this, do a scandir to get an array of the directories then do a foreach on the directory array and do an fwrite to put an index.html file in each.
     
    piehead2000, Sep 27, 2008 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $host = 'localhost';
    $username= 'username';
    $password= 'password';
    $dbname = 'dbname';
    $file_to_copy = '/full/path/to/file/to/copy.php';
    $folder_for_user_dirs = '/home/';
    $filename = basename($file_to_copy);
    
    mysql_connect($host,$username,$password) or die('ERROR WITH DB LOGIN');
    mysql_select_db($dbname) or die('DB NAME "'.$dbname.'" DOES NOT EXIST');
    $query = "SELECT `username_field` FROM `user_table`";
    $res = mysql_query($query);
    if(!$res) die('ERROR IN QUERY');
    
    while($row = mysql_fetch_assoc($res)
    {
       $name = $row['username_field'];
       $folder = $folder_for_user_dirs.substr($name,0,1).'/'.substr($name,1,1).'/';
       mkdir($folder.$name);
       copy($file_to_copy,$folder_for_user_dirs.$name.$filename);
    }
    PHP:
    Something like that
     
    JAY6390, Sep 27, 2008 IP
  4. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Thanks guys! I'm going to give it a try in a few hours and check-back in here. Thank You!!!!!
     
    amelen, Sep 27, 2008 IP
  5. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #5
    a healthy reminder though, is please dont try to run it on a browser, use the command line
     
    serialCoder, Sep 27, 2008 IP