fopen time-out

Discussion in 'PHP' started by pcdoc, May 18, 2006.

  1. #1
    Simple code, of course works fine

    $fp = @fopen($url,"r")
    or exit();

    But, I need more.

    Having searched here and web-wide, can't find a good answer, tried a few, no luck.

    I am wanting to time-out (a simple exit() or die() will suffice) if the $url (or contents) is unreachable.

    It's got me stumped.
     
    pcdoc, May 18, 2006 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I normally do it with a local cache.. that way you can use your cache if the file is unreachable..

    
    $filename = "/home/public_html/cache/filecache.inc.php";
    
    $remotefile = "http://www.remotesite.com/remotefile.html";
    
    if (rand(0,20)==1)
    {
    
            if($newdata = @file_get_contents($remotefile))
            {
    
            
                    $filepointer = fopen($filename, "w");
                    fputs ($filepointer,$newdata);
                    fclose ($filepointer);
            }
    }
    
    $finalvariable = file_get_contents($filename);
    
    PHP:
     
    TheHoff, May 18, 2006 IP