note 39025 added to function.filesize

From: Date: Wed, 14 Jan 2004 22:45:25 +0000
Subject: note 39025 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-63569@lists.php.net to get a copy of this message
i made this function to get the remote size of a file using their header (http). it only works when the server returns the size. i dont know why it happens sometimes and sometimes doesn't. i thought it was better than downloading all the file again and checkin its length. it is a modification of a scritp i have found here at the comments. but that was using fgets to read the headers and was spending too much time (almost 20 seconds at my 256kbps). using fread is much more fast! hope it's useful for someone... <? function remotefilesize($u){ $url = parse_url($u); $host = $url["host"]; $path = $url["path"]; $fp = fsockopen($host, 80, $errno, $errstr, 20); if(!$fp){die("COULD NOT CONNECT TO $host.");} fputs($fp,"HEAD $u HTTP/1.1\r\n"); fputs($fp,"HOST: $host\r\n"); fputs($fp,"Connection: close\r\n\r\n"); $conteudo = explode("\r\n",fread($fp,8192)); //note that 8192 bytes is enough for the header foreach($conteudo as $k=>$v){ if(0){ echo "$v\n"; } //change to 1 if you want to see the header //useful for checkin if the header has the //content-lenght or not if( !(strpos($v,"Content-Length: ") === FALSE) ){ $tamanho = substr($v,16,-2); //16 is the beginning of the line //change to 0 to see yourself //the same for the -2 //it is the end of the line i guess break; } }//foreach fclose($fp); return (isset($tamanho))?$tamanho:0; //if avaliable it returns the filesize //or returns 0 } ?> ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39025 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39025&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39025&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63569) next »