1

I have this code:

require("class.XMLHttpRequest.php");
function hot($news){
 $url="https://localhost/search.aspx?search=".$news.""; 
 $ajax=new XMLHttpRequest();
 $ajax->setRequestHeader("Cookie","Cookie: host");
 $ajax->open("GET",$url,true);
 $ajax->send(null);
 if($ajax->status==200){
  $rHeader=$ajax->getResponseHeader("Set-Cookie");
  if(substr_count($rHeader, "Present!")>0) { return true; }
 }else{ return false; }
} 

$celebrities = array('britney','gaga','carol');
$filename = 'result.txt';
$handle = fopen($filename, 'a');
foreach($celebrities as $celebrity)
{
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); };
}
fclose($handle);

And instead of

$celebrities = array('britney','gaga','carol');
$filename = 'result.txt';
$handle = fopen($filename, 'a');
foreach($celebrities as $celebrity)
{
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); };
}
fclose($handle);

I want to implement

$read_file = fopen('array.txt', 'r');
$write_file = fopen('result.txt', 'a');

while(!feof($read_file))
{
    $celebrity = fgets($read_file);
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }
}

fclose($write_file);
fclose($read_file);

now the result.txt doesnt gets written anymore, where do i go wrong? the script should read an array from a file, process the function hot and then write each reasults in a new line in result.txt thanks in advance!

2 Answers 2

1

Assuming you have a single celeb per line:

$celebrity = trim(fgets($read_file));

As fgets will return a string including the newline character.

Sign up to request clarification or add additional context in comments.

Comments

0

On the line:

if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }

You use $handle, which is no longer defined. You want to be using $write_file instead:

if(hot($celebrity)) { fwrite($write_file, "{$celebrity}\r\n"); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.