Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Pobieranie plików i informacji o nich
Forum PHP.pl > Forum > PHP
koneser69
Witam

Mam taki problem za pomocą polecenia
  1. exec("wget $url")

pobieram pliki na mój serwer.

Jak sprawdzić przed pobraniem jaki rozmiar ma ten plik, i jaką nazwęquestionmark.gif
wookieb
Za pomoca CURL pobierasz same nagłówki.
CURLOPT_HEADER, CURLOPT_NOBODY
cycofiasz
Oczywiście trzeba pobrać nagłówki.
Nie wszystkie strony/aplikacje/serwery tolerują żądanie HEAD, a takie jest wysyłane przy ustawieniu CURLOPT_NOBODY.
Można wykorzystać jednak fsockopen, znalazłem przykładową funkcję:

  1. function HTTP_Get_Header( $url ) {
  2.  
  3. $get_timeout = 40;
  4. $s_Complete = parse_url($url);
  5.  
  6. if( !isset($s_Complete["scheme"] ) )
  7. {
  8. $s_Complete["host"] = $s_Complete["path"];
  9. $s_Complete["path"] = '';
  10. }
  11.  
  12. $s_Host = $s_Complete["host"];
  13. if( @$s_Complete["path"] == "" )
  14. $s_Complete["path"] = "/?";
  15.  
  16. $s_URI = $s_Complete["path"];
  17.  
  18. if( @$s_Complete["query"] != "" )
  19. $s_URI .= '?'.$s_Complete['query'];
  20.  
  21. if( @$s_Complete["port"] != "" )
  22. $s_Port = $s_Complete["port"];
  23. else
  24. $s_Port = 80;
  25.  
  26. $request = "GET $s_URI HTTP/1.0\r\n";
  27. $request .= "Accept: */*\r\n";
  28. $request .= "Cache-Control: no-cache\r\n";
  29. $request .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.2) Gecko/2008090514 Firefox/3.0.2\r\n";
  30. $request .= "Host: $s_Host\r\n";
  31. $request .= "Connection: Close\r\n";
  32. $request .= "\r\n";
  33.  
  34. $fp = @fsockopen( $s_Host, $s_Port, $errno, $errstr, 30 );
  35. if( is_resource($fp) )
  36. {
  37. fputs( $fp, $request );
  38. $query_timeout = 30;
  39. stream_set_blocking( $fp, true );
  40. stream_set_timeout( $fp, $query_timeout );
  41. $loop_time = time();
  42. $status = socket_get_status( $fp );
  43. $line = "";
  44. $header = "";
  45.  
  46. while( !($line == "\r\n") && !feof($fp) && !$status['timed_out'] )
  47. {
  48. $line = fgets($fp, 4096);
  49. $header .= $line;
  50. $diff = time() - $loop_time;
  51. if( $diff > $get_timeout )
  52. break;
  53. break;
  54. $status = socket_get_status( $fp );
  55. }
  56. fclose( $fp );
  57. return $header;
  58. }
  59. return false;
  60. }



Przykład wywołania:

  1. echo HTTP_Get_Header('http://rs1l3.rapidshare.com/files/407970280/RapidShareManager2WindowsSetup.exe');



Z wyciągnięciem potrzebnych danych myślę że już sobie poradzisz
koneser69
Niestety nic nie pomogło. Poniżej szczegóły.

@cycofiasz
funkcja HTTP_Get_Header($URL) zwróciła:

  1. HTTP/1.0 302 Found
  2. Connection: close
  3. X-Powered-By: PHP/5.3.1
  4. Location: <a href="http://www.fileserve.com/landing-1406.html" target="_blank">http://www.fileserve.com/landing-1406.html</a>
  5. Content-type: text/html
  6. Content-Length: 0
  7. Date: Thu, 03 Feb 2011 12:35:43 GMT
  8. Server: lighttpd/1.4.25


@wookieb

  1. $options = array();
  2. $options[CURLOPT_HEADER] = True;
  3. $options[CURLOPT_NOBODY] = True;
  4.  
  5. $c = curl_init($URL);
  6. curl_setopt_array($c,$options);
  7. $result = curl_exec($c);
  8.  
  9. print_r($result);

  1. HTTP/1.1 400 Bad Request
  2. Content-Type: text/html
  3. Content-Length: 349
  4. Connection: close
  5. Date: Thu, 03 Feb 2011 12:39:50 GMT
  6. Server: lighttpd/1.4.25



Jeśli z shella wykonam polecenia

  1. curl -sI $URL


To otrzymuje odpowiednie dane

ale jak w php wykonam

  1. exec(curl -sI $URL, $out, $ret);
  2. print_r($out);
  3. print_r($ret);


to

  1. Array
  2. (
  3. [0] => HTTP/1.1 400 Bad Request
  4. [1] => Content-Type: text/html
  5. [2] => Content-Length: 349
  6. [3] => Connection: close
  7. [4] => Date: Thu, 03 Feb 2011 12:46:21 GMT
  8. [5] => Server: lighttpd/1.4.25
  9. [6] =>
  10. )
  11. 0
wookieb
www.fileserve.com
Czyli mają pewne zabezpieczenia na wywołania botów. A my go omijać nie pomożemy.
Cytat
you agree not to: (...) se any robot, spider, site search/retrieval application (...)
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.