Forum Moderators: coopster

Message Too Old, No Replies

Loading urls from text file

         

DevInteractive

1:06 am on Oct 22, 2004 (gmt 0)

10+ Year Member



hello,

here is what i am trying to do..the script is supposed to open a file called myfile.txt and read the urls from there..each url is in a new line..then its supposed to connect to the site and grab the contents..i am testing the script on a site..right now the problem is it read the first url but doesn't read any after that..any help is much appreciated..thank you

-file myfile.txt-
[example.com...]
[example.com...]
[example.com...]

-/end-


<?php

$filename ="myfile.txt";
$myFile1 = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file
if(! $myFile1){ // Make sure the file was opened successfully
print ("File could not be opened.");
exit;
}

$fcontents = file($filename);
$totalines = count($fcontents); //how many lines of data the file contains
$limit = 5000;
for ($i = 0; $i < $limit; $i++, $totalines--){
$line = $fcontents[$totalines];
if($line!= ""){ // if the line from the file is not blank print it
echo "$line \n";
$url_to_grab = "$line";
$start="<!--START ADVPRINT -->";
$end="<!--END ADVPRINT -->";

if(!($myFile=@fopen($url_to_grab,"r")))
{
echo "The news interface is down for maintenance.";
exit;
}

while(!feof($myFile))
{
// Read each line and add to $myLine
$myLine.=fgets($myFile,255);
}
fclose($myFile);

$start_position=strpos($myLine, $start);
$end_position=strpos($myLine, $end)+strlen($end);
$length=$end_position-$start_position;
$myLine=substr($myLine, $start_position, $length);

echo $myLine;
}

}
fclose($myFile1);
?>

[edited by: coopster at 11:42 am (utc) on Oct. 22, 2004]
[edit reason] generalized urls per TOS [webmasterworld.com] [/edit]

DevInteractive

8:02 am on Oct 23, 2004 (gmt 0)

10+ Year Member



anyone?

charlier

8:40 am on Oct 23, 2004 (gmt 0)

10+ Year Member




I have found that php is sometimes a bit buggy with multple variables in the for loop. Try moving your $totalines-- into the loop body.

DevInteractive

9:49 am on Oct 23, 2004 (gmt 0)

10+ Year Member



you mean like totalines = totalines - 1;?

coopster

5:43 pm on Oct 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, DevInteractive!

Your first issue is in that you are trying to use an index that does not exist. When you count() an array, you will get the number of elements in the array, but remember that array elements begin counting at zero. Therefore, you might change your count() assignment accordingly

$totalines = count($fcontents) - 1; //how many lines of data the file contains

Then, I would also get rid of the newlines since file() is going to keep them in the array elements.

$line = rtrim($fcontents[$totalines]);

Now your if statement should work correctly.

Along another note, you may want to turn error_reporting() [php.net] on, I think you'll find it helpful in debugging.

DevInteractive

7:17 pm on Oct 23, 2004 (gmt 0)

10+ Year Member



cool,

thanks a lot..got it to work!