note 39025 deleted from function.filesize by tomsommer

From: Date: Wed, 14 Jan 2004 23:04:26 +0000
Subject: note 39025 deleted from function.filesize by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-63570@lists.php.net to get a copy of this message
Note Submitter: diogaogv@icqmail.com ---- 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 } ?>

« previous php.notes (#63570) next »