note 61449 added to function.filesize

From: Date: Fri, 03 Feb 2006 16:18:23 +0000
Subject: note 61449 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-102937@lists.php.net to get a copy of this message
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. ---- Server IP: 64.71.164.2 Probable Submitter: 81.56.151.114 ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61449 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61449&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61449&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102937) next »