Hello, I have a problem reading from files, please help me . I have a directory named "car". In that directory(car) I have 30 .txt files which are named differently, for example(mercedes.txt, audi.txt), where each .txt file contain just one line with a number, for example (if you open the audi.txt file you will see just this number: 416.38). With a PHP script I need to open the CAR directory, to read all 30 .txt files(to read the number from each .txt), and after that I must to copy theyr contents(the numbers I have read) into a database table column. PLEASE PLEASE helppppp ! I started like this but I don't know how to continue: <?php $dir=opendir("/car/"); while($file=readdir($dir)){ if ($file!="." and $file!="..") { print "$file<br>"; $lines = file('$file'); foreach ($lines as $line_num => $line){ print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; } } } closedir($dir); ?> It doesn't work. It just shows the .txt files names ...but the next step to show the numbers from files doesn't work (it gives error at foreach loop and show the number from just one file)... Please tell me someone what to do. Thank you in advance.
I took a closer look at it. Try this: <?php $path="/car/"; $dir=opendir($path); while($file=readdir($dir)){ if ($file!="." and $file!="..") { print "$file<br>"; $lines = file($path.$file); foreach ($lines as $line_num => $line){ print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; } } } closedir($dir); ?> PHP:
WONDERFULL it worked!!! Thank you very very much Dagon >< One more thing ...I want every number to copy it into a database table column BUT every number to be placed into different fields of the column not all the numbers into just one field of the column ... PS: I know how to connect to database, I need just these important pieces of code how to insert in a different field every number Thanks a lot!
I have tried the following code but it inserts only the number from last file that I have read in script(only the number from the 30th .txt file is inserted into the column colorprice, but I want all the 30 numbers to be inserted in the column and in different fields of the column ..not all the numbers in the same field of that column). Can anyone help me pls? <?php $path="/car/"; $dir=opendir($path); while($file=readdir($dir)){ if ($file!="." and $file!="..") { print "$file<br>"; $lines = file($path.$file); foreach ($lines as $line_num => $line){ print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; } } } closedir($dir); mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("datab") or die(mysql_error()); mysql_query("INSERT INTO cars (colorprice) VALUES ($line) ") or die(mysql_error()); ?> PHP:
That's my problem..I don't know how to push them into an array ... If I have them into an array I know how to make it. Thnaks!!
Actually are you just trying to insert that into the database? if so just be sure to do it within your foreach
This code works and inserts all the numbers ...but I found that I need arrays because I need to order numbers before I insert them ...I need them to have an order in the column. If I have them in arrays I know how to make this ...but I don't know how to push them into arrays. Thank you <?php $path="/car/"; $dir=opendir($path); while($file=readdir($dir)){ if ($file!="." and $file!="..") { print "$file<br>"; $lines = file($path.$file); foreach ($lines as $line_num => $line){ print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("datab") or die(mysql_error()); mysql_query("INSERT INTO cars (colorprice) VALUES ($line) ") or die(mysql_error()); } } } closedir($dir); ?> PHP:
<?php $path="/car/"; $dir=opendir($path); while($file=readdir($dir)){ if ($file!="." and $file!="..") { $lines[] = $path.$file; } } closedir($dir); ?> PHP:
Hi, You'd better keep keep the connect db and select db methods out of the loop. Put them somewhere on top of your page.