<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: filesize code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 00:08:35 GMT</pubDate>
    <description>DZone Snippets: filesize code</description>
    <item>
      <title>Objective-C and Cocoa: Human-readable file size from number of bytes</title>
      <link>http://snippets.dzone.com/posts/show/3038</link>
      <description>// Returns a human-readable string showing the file size from the number of bytes&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;- (NSString *)stringFromFileSize:(int)theSize&lt;br /&gt;{&lt;br /&gt;	float floatSize = theSize;&lt;br /&gt;	if (theSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%i bytes",theSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;	if (floatSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%1.1f KB",floatSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;	if (floatSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%1.1f MB",floatSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;&lt;br /&gt;	// Add as many as you like&lt;br /&gt;&lt;br /&gt;	return([NSString stringWithFormat:@"%1.1f GB",floatSize]);&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Sun, 26 Nov 2006 05:51:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3038</guid>
      <author>maximile (Max Williams)</author>
    </item>
    <item>
      <title>Get remote filesize (HTTP HEAD)</title>
      <link>http://snippets.dzone.com/posts/show/1207</link>
      <description>// because HTTP HEAD does not work properly in a lot of PHP versions&lt;br /&gt;// can be used to get remote size of a URL (MP3 file, movie, ...)&lt;br /&gt;// without actually downloading the whole thing (e.g. when it's more than 1MB)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function remote_file_size ($url){ &lt;br /&gt;	$head = ""; &lt;br /&gt;	$url_p = parse_url($url); &lt;br /&gt;	$host = $url_p["host"]; &lt;br /&gt;	if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){&lt;br /&gt;		// a domain name was given, not an IP&lt;br /&gt;		$ip=gethostbyname($host);&lt;br /&gt;		if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){&lt;br /&gt;			//domain could not be resolved&lt;br /&gt;			return -1;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	$port = intval($url_p["port"]); &lt;br /&gt;	if(!$port) $port=80;&lt;br /&gt;	$path = $url_p["path"]; &lt;br /&gt;	//echo "Getting " . $host . ":" . $port . $path . " ...";&lt;br /&gt;&lt;br /&gt;	$fp = fsockopen($host, $port, $errno, $errstr, 20); &lt;br /&gt;	if(!$fp) { &lt;br /&gt;		return false; &lt;br /&gt;		} else { &lt;br /&gt;		fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n"); &lt;br /&gt;		fputs($fp, "HOST: " . $host . "\r\n"); &lt;br /&gt;		fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");&lt;br /&gt;		fputs($fp, "Connection: close\r\n\r\n"); &lt;br /&gt;		$headers = ""; &lt;br /&gt;		while (!feof($fp)) { &lt;br /&gt;			$headers .= fgets ($fp, 128); &lt;br /&gt;			} &lt;br /&gt;		} &lt;br /&gt;	fclose ($fp); &lt;br /&gt;	//echo $errno .": " . $errstr . "&lt;br /&gt;";&lt;br /&gt;	$return = -2; &lt;br /&gt;	$arr_headers = explode("\n", $headers); &lt;br /&gt;	// echo "HTTP headers for &lt;a href='" . $url . "'&gt;..." . substr($url,strlen($url)-20). "&lt;/a&gt;:";&lt;br /&gt;	// echo "&lt;div class='http_headers'&gt;";&lt;br /&gt;	foreach($arr_headers as $header) { &lt;br /&gt;		// if (trim($header)) echo trim($header) . "&lt;br /&gt;";&lt;br /&gt;		$s1 = "HTTP/1.1"; &lt;br /&gt;		$s2 = "Content-Length: "; &lt;br /&gt;		$s3 = "Location: "; &lt;br /&gt;		if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1)); &lt;br /&gt;		if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2));  &lt;br /&gt;		if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));  &lt;br /&gt;		} &lt;br /&gt;	// echo "&lt;/div&gt;";&lt;br /&gt;	if(intval($size) &gt; 0) {&lt;br /&gt;		$return=intval($size);&lt;br /&gt;	} else {&lt;br /&gt;		$return=$status;&lt;br /&gt;	}&lt;br /&gt;	// echo intval($status) .": [" . $newurl . "]&lt;br /&gt;";&lt;br /&gt;	if (intval($status)==302 &amp;&amp; strlen($newurl) &gt; 0) {&lt;br /&gt;		// 302 redirect: get HTTP HEAD of new URL&lt;br /&gt;		$return=remote_file_size($newurl);&lt;br /&gt;	}&lt;br /&gt;	return $return; &lt;br /&gt;} &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 20 Jan 2006 02:48:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1207</guid>
      <author>pforret (Peter Forret)</author>
    </item>
  </channel>
</rss>
