note 64450 modified in function.filesize by danbrown

From: Date: Sun, 18 Jan 2009 18:09:30 +0000
Subject: note 64450 modified in function.filesize by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149416@lists.php.net to get a copy of this message
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 ) { // failed to open TCP socket connection // do something sensible here besides exit(); echo "<br>failed to open socket for [$ipAddress]"; exit(); } // just send HEAD request to server $head = "HEAD $url HTTP/1.0\r\nConnection: Close\r\n\r\n"; // you may use HTTP/1.1 instead, then your request head string _must_ contain "Host: " header fwrite( $socket, $head ); // read the response header $header = ReadHeader( $socket ); if( !$header ) { // handle empty response here the way you need... Header( "HTTP/1.1 404 Not Found" ); exit(); } fclose( $socket ); // check for "Location" header $locationMarker = "Location: "; $pos = strpos( $header, $locationMarker ); if( $pos > 0 ) { $counter302--; if( $counter302 < 0 ) { // redirect limit (5 by default) expired -- return some warning or do something sensible here echo "warning: too long redirection sequence"; return 0; } // Location is present -- we should determine target host and move there, like any downloading masters do... // no need to use regex here $end = strpos( $header, "\n", $pos ); $location = trim( substr( $header, $pos + strlen( $locationMarker ), $end - $pos - strlen( $locationMarker ) ), "\\r\\n" ); // extract pure host (without "http://") $host = explode( "/", $location ); $ipa = gethostbyname( $host[2] ); // move to Location return remote_filesize_thru( $ipa, $location, $counter302 ); } // try to acquire Content-Length within the response $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $header, $matches); // if there was a Content-Length field, its value // will now be in $matches[1] if( isset( $matches[1] ) ) $size = $matches[1]; else $size = 0; return $size; } ?> --was-- 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 ) { // failed to open TCP socket connection // do something sensible here besides exit(); echo "<br>failed to open socket for [$ipAddress]"; exit(); } // just send HEAD request to server $head = "HEAD $url HTTP/1.0\r\nConnection: Close\r\n\r\n"; // you may use HTTP/1.1 instead, then your request head string _must_ contain "Host: " header fwrite( $socket, $head ); // read the response header $header = ReadHeader( $socket ); if( !$header ) { // handle empty response here the way you need... Header( "HTTP/1.1 404 Not Found" ); exit(); } fclose( $socket ); // check for "Location" header $locationMarker = "Location: "; $pos = strpos( $header, $locationMarker ); if( $pos > 0 ) { $counter302--; if( $counter302 < 0 ) { // redirect limit (5 by default) expired -- return some warning or do something sensible here echo "warning: too long redirection sequence"; return 0; } // Location is present -- we should determine target host and move there, like any downloading masters do... // no need to use regex here $end = strpos( $header, "\n", $pos ); $location = trim( substr( $header, $pos + strlen( $locationMarker ), $end - $pos - strlen( $locationMarker ) ), "\\r\\n" ); // extract pure host (without "http://") $host = explode( "/", $location ); $ipa = gethostbyname( $host[2] ); // move to Location return remote_filesize_thru( $ipa, $location, $counter302 ); } // try to acquire Content-Length within the response $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $header, $matches); // if there was a Content-Length field, its value // will now be in $matches[1] if( isset( $matches[1] ) ) $size = $matches[1]; else $size = 0; return $size; } http://php.net/manual/en/function.filesize.php

« previous php.notes (#149416) next »