some notes and modifications to previous post.
refering to RFC, when using HTTP/1.1 your request (either GET or POST or HEAD) must contain Host header string, opposite to HTTP/1.1 where Host ain't required. but there's no sure how your remote server would treat the request so you can add Host anyway (it won't be an error for HTTP/1.0).
host value _must_ be a host name (not CNAME and not IP address).
this function catches response, containing Location header and recursively sends HEAD request to host where we are moved until final response is met.
(you can experience such redirections often when downloading something from php scripts or some hash links that use apache mod_rewrite. most all of dowloading masters handle 302 redirects correctly, so this code does it too (running recursively thru 302 redirections).)
[$counter302] specify how much times your allow this function to jump if redirections are met. If initial limit (5 is default) expired -- it returns 0 (should be modified for your purposes whatever).0
ReadHeader() function is listed in previous post
(param description is placed there too).
<?php
function remote_filesize_thru( $ipAddress, $url, $counter302 = 5 )
{
$socket = fsockopen( "10.233.225.2", 8080 );
if( !$socket )
{
echo "<br>failed to open socket for [$ipAddress]";
exit();
}
$head = "HEAD $url HTTP/1.0\r\nConnection: Close\r\n\r\n";
fwrite( $socket, $head );
$header = ReadHeader( $socket );
if( !$header )
{
Header( "HTTP/1.1 404 Not Found" );
exit();
}
fclose( $socket );
$locationMarker = "Location: ";
$pos = strpos( $header, $locationMarker );
if( $pos > 0 )
{
$counter302--;
if( $counter302 < 0 )
{
echo "warning: too long redirection sequence";
return 0;
}
$end = strpos( $header, "\n", $pos );
$location = trim( substr( $header, $pos + strlen( $locationMarker ), $end - $pos - strlen( $locationMarker ) ), "\\r\\n" );
$host = explode( "/", $location );
$ipa = gethostbyname( $host[2] );
return remote_filesize_thru( $ipa, $location, $counter302 );
}
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $header, $matches);
if( isset( $matches[1] ) )
$size = $matches[1];
else
$size = 0;
return $size;
}
?>