0

I have the following function :

    function doParse($parser_object) {
    $links=file("./fullsoccer.TXT", "r");
    $i=0;
    while(!empty($links[$i]))
    {
        set_time_limit(0);
        if (!($fp = fopen($links[$i], "r")));
        {
            //loop through data
            while ($data = fread($fp, 4096)) {
                //parse the fragment
                xml_parse($parser_object, $data, feof($fp));
            }
        }
        $i++;
    }
}

This code save a list of 507 links of XML data from fullsocce.txt into $links then I read the content of each file (link: online links) using fread and pass the $data to my main function which is xml_parse to parse and save the data using SAX parser . my problem is: just that last file of the array $links is passed to the function and parse the data , I want your help to know why it is working just with one file? please It is an emergency case

unique_programmer
  • 1,561
  • 3
  • 11
  • 14

2 Answers2

0
!($fp = fopen($links[$i], "r")

fopen returns FALSE on error so this check is only true at eof or an invild line. Maybe changing it to

 if (($fp = fopen($links[$i], "r") != FALSE)

I also noticed an extra ; and ) in if (!($fp = fopen($links[$i], "r"))); Maybe I'm just not matching properly.

Useless Intern
  • 1,294
  • 1
  • 10
  • 20
  • I try making some debug. first of all the code is printing all links which means it is opening them and when I use "xml_parse" it is just parsing the last file in the links array why is that?? do you have any idea ?? – unique_programmer May 09 '13 at 06:19
0

you are missing fclose, but other than that I think you need to do some debugging, try comment out the "xml_parse" function and add echo $links[$i]."<br />\n";

function doParse($parser_object) {
  $links=file("./fullsoccer.TXT", "r");
  $i=0;
  foreach($links as $link)
  {
      set_time_limit(0);
      echo 'reading '.$link."\n";
      $fp = fopen($link, "r");
      if ($fp!==false)
      {
          //loop through data
          while ($data = fread($fp, 4096)) {
              //parse the fragment
              //xml_parse($parser_object, $data, feof($fp));
              echo $data;
          }
          echo "\n";
          fclose($fp);
      } else {
          echo 'Cannot Open Link '.$link."\n";
      }
      $i++;
  }
}
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
  • I try making some debug. first of all the code is printing all links which means it is opening them and when I use "xml_parse" it is just parsing the last file in the links array why is that?? do you have any idea ?? – unique_programmer May 09 '13 at 06:18
  • Do you see the `$data` returned for each file ? If so it might be that your `xml_parse` function has issues, if not it means that the files might be empty – Fabrizio May 09 '13 at 13:44
  • Actually, I do think that the problem might be in your `xml_parse`, I only assume that you open/close a file, could it be that every time you open you wipe it clean causing it to show only the last file ? – Fabrizio May 09 '13 at 13:47