Поиграть


Фотографии знаменитостей, Герои нашего времени

Получить размер файла на другом сервере, php

Code (php)
  1.  
  2. function remote_file_size ($url){
  3.         $head = "";
  4.         $url_p = parse_url($url);
  5.         $host = $url_p["host"];
  6.         if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){
  7.                 // a domain name was given, not an IP
  8.                 $ip=gethostbyname($host);
  9.                 if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){
  10.                         //domain could not be resolved
  11.                         return -1;
  12.                 }
  13.         }
  14.         $port = intval($url_p["port"]);
  15.         if(!$port) $port=80;
  16.         $path = $url_p["path"];
  17.         //echo "Getting " . $host . ":" . $port . $path . " …";
  18.  
  19.         $fp = fsockopen($host, $port, $errno, $errstr, 20);
  20.         if(!$fp) {
  21.                 return false;
  22.                 } else {
  23.                 fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n");
  24.                 fputs($fp, "HOST: " . $host . "\r\n");
  25.                 fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
  26.                 fputs($fp, "Connection: close\r\n\r\n");
  27.                 $headers = "";
  28.                 while (!feof($fp)) {
  29.                         $headers .= fgets ($fp, 128);
  30.                         }
  31.                 }
  32.         fclose ($fp);
  33.         //echo $errno .": " . $errstr . "
  34. ";
  35.         $return = -2;
  36.         $arr_headers = explode("\n", $headers);
  37.         // echo "HTTP headers for <a href=‘" . $url . "’>…" . substr($url,strlen($url)-20). "</a>:";
  38.         // echo "<div class=‘http_headers’>";
  39.         foreach($arr_headers as $header) {
  40.                 // if (trim($header)) echo trim($header) . "
  41. ";
  42.                 $s1 = "HTTP/1.1";
  43.                 $s2 = "Content-Length: ";
  44.                 $s3 = "Location: ";
  45.                 if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
  46.                 if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2)); 
  47.                 if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3)); 
  48.                 }
  49.         // echo "</div>";
  50.         if(intval($size) > 0) {
  51.                 $return=intval($size);
  52.         } else {
  53.                 $return=$status;
  54.         }
  55.         // echo intval($status) .": [" . $newurl . "]
  56. ";
  57.         if (intval($status)==302 && strlen($newurl) > 0) {
  58.                 // 302 redirect: get HTTP HEAD of new URL
  59.                 $return=remote_file_size($newurl);
  60.         }
  61.         return $return;
  62. }
  63.  

+1+2+3+4+5 (Ещё не голосовали)
Загрузка... Загрузка...

Оставьте комментарий

Блог разработчика / Недвижимость за границей русские за границей русские за границей!
центр ремонт ноутбуков братеево москва
kvchosting
Large selection of free magento templates 1.4.x with Live Demo!