...

View Full Version : Help reading .txt files and parsing to DB?



stfc_boy
03-11-2010, 06:15 PM
Hi,

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

http://www.sloughtownfc.net/images_send/one.jpg

http://www.sloughtownfc.net/images_send/two.jpg

http://www.sloughtownfc.net/images_send/three.jpg

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

View Screen (http://www.sloughtownfc.net/parser_test.php)

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

http://www.sloughtownfc.net/images_send/layout.jpg

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

/* 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>';

}

?>

Fou-Lu
03-11-2010, 06:23 PM
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

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.

stfc_boy
03-11-2010, 06:36 PM
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?

Fou-Lu
03-11-2010, 06:42 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum