Télécharger régulièrement un fichier.
Ici un exemple de téléchargement de carte des températures chaque 12 heures
Download a file periodically.
Here is an example of downloading map temperatures every 12 hours
تنزيل ملف بشكل دوري.
هنا مثال عن تنزيل خريطة درجة الحرارة كل ١٢ ساعة
Open in a new window
<?php //############################################################## // more scripts on scripts-n-codes.blogspot.com //############################################################## // // frequently download a file // for example here // File to download $remoteFile = 'http://www.wzkarten3.de/pics/Reurmett.gif'; // Local file for saving $localFile = "weather.gif"; // Time to cache in hours $cacheTime = 12; // Connection time out $connTimeout = 10; if(!(file_exists($localFile) && (time() - ($cacheTime * 3600) < filemtime($localFile)))){ $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\r\n"; $header .= "Host: $host\r\n"; $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\r\n"; $header .= "Accept: */*\r\n"; $header .= "Accept-Language: en-us,en;q=0.5\r\n"; $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"; $header .= "Keep-Alive: 300\r\n"; $header .= "Connection: keep-alive\r\n"; $header .= "Referer: http://$host\r\n\r\n"; $response = ''; fputs($fp, $header); // Get the file content while($line = fread($fp, 4096)){ $response .= $line; } fclose( $fp ); // Remove Header Info $pos = strpos($response, "\r\n\r\n"); $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); } } } } ?>