Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Help reading .txt files and parsing to DB?

    Hi,

    I'm looping for some help. I've written a script which looks into a folder for .txt files (below)







    ...and prints the contents of what's in each file to the screen like so:

    View Screen

    Now what I want to do is take these details and for each file insert them row-by-row in the database like so:



    But i'm not sure how to do this part of it where it inserts these details in the database. The rest of it works great. Can anyone kindly help and help me with this code?

    Thanks

    Chris

    PHP Code:
    <?php 

    /* Define and Open the Directory */
    $path    '/home/******/parsers/******/write/';
    $dir    opendir($path);

    /* Loop through the directory using a while loop if $dir exists */
    while (false !== ($file readdir($dir))) {

    //Read the files in the directory: one.txt, two.txt, three.txt
    $the_text file_get_contents($path.$file);

      
    //Now need to insert each new line in the database but not sure how???
      
    $SQL "INSERT INTO test_table (phrase,phrase2,phrase3) VALUES ('$line_one','$line_two','$line_three')";
      
    $QUE mysql_query($SQL);
      
    echo 
    '<pre>'print_r($the_text); echo '</pre>';
      
    }

    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Looks correct to me, you'll need to take care of some sanitation though. The problem is that $line_one, $line_two, $line_three do not exist, so we'll create them using list in conjunction with an explode:
    PHP Code:
    <?php  

    if (function_exists('set_magic_quotes_runtime'))
    {
        
    set_magic_quotes_runtime(0);
    }

    /* Define and Open the Directory */ 
    $path    '/home/******/parsers/******/write/'
    $dir    opendir($path); 


    /* Loop through the directory using a while loop if $dir exists */ 
    while (false !== ($file readdir($dir))) { 

        
    //Read the files in the directory: one.txt, two.txt, three.txt 
        
    $the_text file_get_contents($path.$file); 
        list(
    $line_one$line_two$line_three) = explode(PHP_EOL$the_text);
        
    //Now need to insert each new line in the database but not sure how??? 
        
    $SQL "INSERT INTO test_table (phrase,phrase2,phrase3) VALUES ('$line_one','$line_two','$line_three')"
        
    $QUE mysql_query($SQL); 
       
        echo 
    '<pre>'print_r($the_text); echo '</pre>';    
    }
    Try that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    stfc_boy (03-11-2010)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Awesome - looks good to me - thanks. Out of interest any other way you could do it rather than using PHP_EOL? Just thinking for example if I wanted to grab the second line only of a set line number of each text file?
    Last edited by stfc_boy; 03-11-2010 at 05:40 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP_EOL is just the linebreak in PHP. That will capture each line into its own variable.
    If you instead use the file() command over the file_get_contents, it will automatically parse it into an array and the second line will always be $results[1]. This is fine if you're file size is always going to be small; I wouldn't recommend using file with large amounts of data as you'll find that it will quickly exhaust the memory available for you to use.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •