Jump to content

Help with writing to file


msimonds

Recommended Posts

I am trying to write data to an HTML file.  I want this to perform only once every 24 hours.  I am having issues with the following code:

 

<?php
error_reporting(E_ALL & ~E_NOTICE);

$html_file = "nba_scores.html";
$cache_reload_seconds = 86400;

if (file_exists($html_file))
{
    if ((filemtime($html_file) + ($cache_reload_seconds - 90)) > time())
    { 
        $test = write_to_html($html_file);      
    }
    else
    {
        unlink($html_file);
    }
}

function write_to_html($file)
{ 
include_once ('class_ScoreScraper.php');
    $ss = new ScoreScraper();
    $league = 'nba';

    
    $ss->fetchScore($league);
    foreach ($ss->scores as $score)
    {
        if ($score['isStarted'])
        {
            $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['homeTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['homeTeamScore']}</strong></div></td>\n</tr>\n";
            $nba_output .= "<tr>\n<td class='alt1'><div class='smallfont'><strong>{$score['awayTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['awayTeamScore']}</strong></div></td>\n</tr>\n";
            $nba_output .= "<tr>\n<td class='alt1'> </td><td align='right' class='alt1'> </td>\n</tr>\n";

        }
        else
        {
            $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['homeTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>Game Time</strong></div></td>\n</tr>\n";
            $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['awayTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['gameTime']}</strong></div></td>\n</tr>\n";
            $nba_output .= "<tr>\n<td class='alt1'> </td><td align='right' class='alt1'> </td>\n</tr>\n";
        }
    }
    file_put_contents($file,$nba_output);
    return $file;
}
?>

 

Problem is that if the file is over 24 hours, the script runs and then deletes the file and then never recreates it because of my stupid code

 

Can someone please help me out

 

TIA,

Mike

Link to comment
https://forums.phpfreaks.com/topic/96702-help-with-writing-to-file/
Share on other sites

I think the logic shud be

if (!(file_exists($html_file) && ((filemtime($html_file) + ($cache_reload_seconds - 90)) < time()) ))
{ 
     if(file_exists($html_file)) unlink($html_file);
     $test = write_to_html($html_file);      
}

 

file exists and time less than expiry (which in case we dun want to do anything)

which wud be a valid, hey we got our cache and it hasnt expired

but the ! in front of the conditionals make it everything else :)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.