note 47552 added to function.filesize

From: Date: Fri, 19 Nov 2004 23:33:43 +0000
Subject: note 47552 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-80786@lists.php.net to get a copy of this message
In addition to the handy function Kris posted, here is an upgraded version that does basic http authentication as well. <?php /* * (mixed)remote_filesize($uri,$user='',$pw='') * returns the size of a remote stream in bytes or * the string 'unknown'. Also takes user and pw * incase the site requires authentication to access * the uri */ function remote_filesize($uri,$user='',$pw='') { // start output buffering ob_start(); // initialize curl with given uri $ch = curl_init($uri); // make sure we get the header curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here if (!empty($user) && !empty($pw)) { $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $okay = curl_exec($ch); curl_close($ch); // get the output buffer $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings ob_end_clean(); // gets you the numeric value from the Content-Length // field in the http header $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $head, $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 = 'unknown'; } return $size; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47552 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47552&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47552&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80786) next »