note 64450 added to function.filesize

From: Date: Fri, 14 Apr 2006 15:21:56 +0000
Subject: note 64450 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-108086@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; } ---- Server IP: 66.163.161.117 Probable Submitter: 213.184.229.145 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/note/edit/64450 Del: integrated -- http://master.php.net/note/delete/64450/integrated Del: useless -- http://master.php.net/note/delete/64450/useless Del: bad code -- http://master.php.net/note/delete/64450/bad+code Del: spam -- http://master.php.net/note/delete/64450/spam Del: non-english -- http://master.php.net/note/delete/64450/non-english Del: in docs -- http://master.php.net/note/delete/64450/in+docs Del: other reasons-- http://master.php.net/note/delete/64450 Reject -- http://master.php.net/note/reject/64450 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#108086) next »