Jump to content

Recommended Posts

This has me confused.

 

 

I've posted the code here for easy reading

 

<?php
# ----------------------------------------------------------------------------------
# Configuration

# Directory on the remote server.  Be sure to include leading and trailing slashes!
$remotedir = "/pub/data/raw/wt/";

# File on the remote server.  No slashes!
$remotefile = "wtnt31.knhc.tcp.at1.txt";

# Keyword to check for in response.
$keyword = "$$";

# Remote server address.
$remoteserver = "weather.noaa.gov";

# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "REMOVED";

# E-mail subject line.
$emailsubject = "[NHC] WTNT31 -";

# E-mail From name.
$emailfromname = "REMOVED";

# E-mail From address.
$emailfromaddr = "REMOVED";

$txtfilename = $remotefile;
# End Configuration
# ----------------------------------------------------------------------------------

# Format the page for easy viewing.
Header( "Content-type: text/plain");

# Setup the request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $remoteserver . $remotedir . $remotefile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res = curl_exec($ch);
curl_close($ch);
$result = array_map('trim',explode("\n",$res));
# Check for keyword.
if (!in_array($keyword,$result)) die("Weather Coding not found!");


# Read the file containing the last response.
$filename = $txtfilename . '.txt';
if (file_exists($filename)) {
$contents = array_map('trim',file($filename));
# Check for changes.
if (count(array_diff(array_unique($contents),array_unique($result))) == 0) {
	# There has not been a change.
	echo ("No Updates\r\n");
} else {
	# There has been a change.
	echo ("**UPDATES DETECTED**\r\n");
	# Write the new file.
	echo implode("\n",$result);
	file_put_contents($filename,implode("\n",$result));
	        $subject = $emailsubject . $result[3];
# Send the e-mail.
        $recipient = $emailrecip;
        $body_of_email = implode("\n",$result);

        $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
        $header .= "\n"; 
        $header .= "X-Priority: 1";
        $header .= "\n";

	        mail ($recipient, $subject, $body_of_email, $header);

        echo ("E-mail sent.");
}
} else {		
# Write the new file.
echo implode("\n",$result);
file_put_contents($filename,implode("\n",$result));
}
?>

 

This is what it is doing and it's confusing me.

 

If the text on the webserver changes the script runs normally and writes to the file and emails.

 

If I delete the .txt from my server the script SHOULD run normally and send out an email and write a new file.

 

What it is doing is writing is skipping the email part, and jumping right to this part.

 

else {

# Write the new file.

echo implode("\n",$result);

file_put_contents($filename,implode("\n",$result));

}

 

It's as if it is reading from a buffer because it's not reading from the .txt file because I deleted it but writes the txt file back to the drive.

 

What am i missing to get it to run normally?

 

Windows 2008 64bit R2, IIS7 and PHP FastCGI 5.1.3

Link to comment
https://forums.phpfreaks.com/topic/206251-php-script-not-doing-what-it-should/
Share on other sites

You're only sending email when there is an old file present on your drive and there is a change. You have to change your logic a little.

 

Something like this:

<?php
$contents = (file_exists($filename))?array_map('trim',file($filename)):array();
?>

Then remove the final "else" block.

 

This would make an empty $contents array if the file wasn't there and the rest of your logic should still work.

 

Ken

It should be sending email IF the file on my server does not match the file being pulled OR does not exist at all on my server.

 

But it is as if it's reading a buffer or cache from someplace else. if I delete the file on my server and try to trigger the script it runs like the file is there.

That's not how you wrote it. You're logic says

if the file is there {
   if there are changes {
       write new file
       send email
   }
} else {
   write new file
}

what you want

if file is not there, create an empty $contents array, else read the current file into the $contents array
if there are changes {
   write new file
   send email
}

 

Ken

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.