can i do that in php

Discussion in 'HOWTO-Related Questions' started by qwe010, Sep 13, 2008.

  1. qwe010

    qwe010 New Member

    hello

    i have

    text file names.txt

    inside it

    many lines ( names )

    can i creat php code to

    read the file and put all the names in database ?
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    Thats relatively easy:

    Code:
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_select_db('mydb', $link);
    $lines = file("/path/to/your.txt");
    foreach($lines as $line) {
      $name = trim($line);
      mysql_query("INSERT INTO mytable (myname) VALUES ('$name')",$link);
    }
    mysql_close($link);
    
     
  3. qwe010

    qwe010 New Member

    thank you very much

    but if the file to big

    how can i make the insert in the database on parts

    and how i know if the process done without error
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    How big? If its not larger then say 100 or 100 MB, this should work. You will just have to set the memory limit in PHP to a higher value.

    Otherwise youz will have to take a look in the PHP documentation, you will find there als file functions to read a file line by line.
     
  5. qwe010

    qwe010 New Member

    just 2 gb :p

    i did but i don't find any thing

    but i searched in google and i found that
    PHP:
    $file "file.txt";
    $fh fopen($file"rb");

    //feof = end of file
    while(!feof($fh)) {
      
    $line fgets($fh); //fgets reads one line at a time

      //do something

    PHP:
    $read_lim 8192//size in bytes to load into memory
    $file "test.txt";
    $fh fopen($file"rb");
    $size filesize($file); //total file size

    while ($size 0) {
      
    $rlen = ($size $read_lim) ? $read_lim $size//read length
      
    $buffer fread($fh$rlen);

      
    //do something

      
    $size -= $rlen;

    what the best ?
     
  6. qwe010

    qwe010 New Member

    up any help please
     
  7. qwe010

    qwe010 New Member

    up any help please
     

Share This Page