Revision: 19963
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 4, 2009 08:21 by xida
Initial Code
// File to download $remoteFile = 'http://www.yahoo.com/'; // Local file for saving $localFile = "YahooHome.htm"; // Time to cache in hours $cacheTime = 24; // Connection time out $connTimeout = 10; if(file_exists($localFile) && (time() - ($cacheTime * 3600) < filemtime($localFile))){ readfile($localFile); }else{ $url = parse_url($remoteFile); $host = $url['host']; $path = isset($url['path']) ? $url['path'] : '/'; if (isset($url['query'])) { $path .= '?' . $url['query']; } $port = isset($url['port']) ? $url['port'] : '80'; $fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout ); if(!$fp){ // If connection failed, return the cached file if(file_exists($localFile)){ readfile($localFile); } }else{ // Header Info $header = "GET $path HTTP/1.0 "; $header .= "Host: $host "; $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6 "; $header .= "Accept: */* "; $header .= "Accept-Language: en-us,en;q=0.5 "; $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 "; $header .= "Keep-Alive: 300 "; $header .= "Connection: keep-alive "; $header .= "Referer: http://$host "; $response = ''; fputs($fp, $header); // Get the file content while($line = fread($fp, 4096)){ $response .= $line; } fclose( $fp ); // Remove Header Info $pos = strpos($response, " "); $response = substr($response, $pos + 4); echo $response; // Save the file content if(!file_exists($localFile)){ // Create the file, if it doesn't exist already fopen($localFile, 'w'); } if(is_writable($localFile)) { if($fp = fopen($localFile, 'w')){ fwrite($fp, $response); fclose($fp); } } } }
Initial URL
Initial Description
Initial Title
Remote File Saving
Initial Tags
Initial Language
PHP