Read from FILE

Discussion in 'PHP' started by emi87, Dec 28, 2007.

  1. #1
    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.
     
    emi87, Dec 28, 2007 IP
  2. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    replace
    $lines = file('$file');
    by
    $lines = file($file);
     
    Dagon, Dec 28, 2007 IP
  3. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it gives me the same error
     
    emi87, Dec 28, 2007 IP
  4. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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:
     
    Dagon, Dec 28, 2007 IP
  5. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    WONDERFULL :D it worked!!!
    Thank you very very much Dagon >:D<
    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!
     
    emi87, Dec 28, 2007 IP
  6. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    emi87, Dec 29, 2007 IP
  7. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Push them into an array and use a foreach loop
     
    marshall_26, Dec 29, 2007 IP
  8. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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!!
     
    emi87, Dec 29, 2007 IP
  9. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Actually are you just trying to insert that into the database? if so just be sure to do it within your foreach
     
    marshall_26, Dec 29, 2007 IP
  10. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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:
     
    emi87, Dec 29, 2007 IP
  11. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    <?php
    $path="/car/";
    $dir=opendir($path);
    while($file=readdir($dir)){
    if ($file!="." and $file!="..") {
      $lines[] = $path.$file;
      }
    }
    closedir($dir);
    ?>
    PHP:
     
    marshall_26, Dec 29, 2007 IP
  12. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thank you a lot! >:D< You saved me :D
     
    emi87, Dec 29, 2007 IP
  13. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Your welcome, you could have used array_push too but I prefer the method above.
     
    marshall_26, Dec 29, 2007 IP
  14. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #14
    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.
     
    Dagon, Dec 29, 2007 IP
  15. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ok, thank you very much Dagon. >:D<
     
    emi87, Dec 29, 2007 IP