note 61449 deleted from function.filesize by derick

From: Date: Thu, 23 Feb 2006 14:22:57 +0000
Subject: note 61449 deleted from function.filesize by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-104301@lists.php.net to get a copy of this message
Note Submitter: samuel dot l at mushicrew dot com ---- If you can't, for any reason, use the CURL extension on your server, you can use this function to request filesize on HTTP servers. <?php function http_filesize($sURL, $nTimeout = 30) { // Parsing URL $aURL = parse_url($sURL); // Checking HTTP request if($aURL['scheme'] != 'http') return false; // HTTP Host $sHost = $aURL['host']; // Remote file path $sFilepath = (isset($aURL['path']) ? $aURL['path'] : '/') . (isset($aURL['query']) ? '?' . $aURL['query'] : ''); // Remote port $nPort = isset($aURL['port']) ? $aURL['port'] : 80; // Socket opening $fpRemote = fsockopen($sHost, $nPort, $errno, $errstr, $nTimeout); // Make sure the socket is open if(!$fpRemote) return false; else { // Request headers $sHeaders = "HEAD " . $sFilepath . " HTTP/1.1\r\n"; $sHeaders .= "Host: ". $sHost . "\r\n"; $sHeaders .= "Connection: Close\r\n\r\n"; // Sending headers fwrite($fpRemote, $sHeaders); // Getting back the content $sRemoteHeaders = ''; while(!feof($fpRemote)) $sRemoteHeaders .= fgets($fpRemote, 128); // Closing the socket fclose($fpRemote); // Parsing the headers preg_match('/Content-Length:\s([0-9].+?)\s/', $sRemoteHeaders, $aMatches); if(isset($aMatches[1])) return (int)$aMatches[1]; else return false; } } ?> Not that this function will return FALSE if the socket can't connect, or if the server doesn't provide the Content-Length header.

« previous php.notes (#104301) next »