Submit Text File Contents To Database

Discussion in 'PHP' started by poblo10, Oct 24, 2010.

  1. #1
    So I want to submit like 1million+ words to a database from a text file.

    The text files words are in a format of

    word1
    word2
    word3

    So each word is after a linebreak-enter-/n.

    So if someone could give me an example of a script that has the capability to submit over 1million words from a text file that would be great!
     
    poblo10, Oct 24, 2010 IP
  2. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So you have a single text file with words one after another? So maybe something like this with fopen?

    
    <?php
    
    $filename = "c:\\folder\\resource.txt";
    
    $file = fopen($filename, "w+"); 
    
    if(!$file) return false;
    
    $data = file_get_contents($filename);
    fclose($file);
    
    $data = explode("\n", $data);
    
    foreach($data as $value) {
        $db->query("INSERT INTO `db`(info) VALUES('" . $value . "')");
    }
    
    ?>
    
    
    PHP:
    Not sure if file_get_contents shows \n as a return carriage you may have to improvise here. Also note that unless you have tweaked your PHP ini settings, a million entries is likely to time-out. I suggest doing it in smaller bursts.

    Hope it helps.
     
    KingOle, Oct 24, 2010 IP