<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dawnerd &#187; traffic shaping</title>
	<atom:link href="http://dawnerd.com/tag/traffic-shaping/feed/" rel="self" type="application/rss+xml" />
	<link>http://dawnerd.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Fri, 13 Aug 2010 18:20:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Basic Traffic Shaping in PHP</title>
		<link>http://dawnerd.com/post/5_basic-traffic-shaping-in-php/</link>
		<comments>http://dawnerd.com/post/5_basic-traffic-shaping-in-php/#comments</comments>
		<pubDate>Wed, 07 May 2008 08:18:57 +0000</pubDate>
		<dc:creator>Troy Whiteley</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[traffic shaping]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=5</guid>
		<description><![CDATA[I ran into this problem today where we needed a PHP based solution to limit file download speeds. There are probably better ways to do this, so it might be wise to consider your possibilities before continuing. It is amazingly &#8230; <a href="http://dawnerd.com/post/5_basic-traffic-shaping-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I ran into this problem today where we needed a PHP based solution to limit file download speeds. There are probably better ways to do this, so it might be wise to consider your possibilities before continuing. <span id="more-5"></span></p>
<p>It is amazingly simple to limit download speeds with php using no more than a half-handful of built in functions.</p>
<p>To get started, define the download path, filename, and download speed.</p>
<pre>
<code markup="none">
$file_path = "/path/to/file/demosong.mp3";
$file_name = "Cool_Song.mp3";
//download speed is in kb/s
$download_rate = 50;
</code>
</pre>
<p><em>note: you can easily change the download rate on a per-mimetype basis. ex:</em></p>
<pre>
<code markup="none">
if(strstr($file_name,'.mp3'))
{
$download_rate = 30;
}
else
{
$download_rate = 150;
}
</code>
</pre>
<p>Now, check that the file exists and then send some header information.</p>
<pre>
<code markup="none">
if(file_exists($file_path) &#038;&#038; is_file($file_path))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file_path));
header('Content-Disposition: filename='.$file_name);
flush();
</code>
</pre>
<p><em>note: it is a good idea to leave the <code>flush()</code> in there.</em></p>
<p>All we have to do now is read the file in chunks. PHP will sleep every second and output the <code>$download_rate</code> to the browser.</p>
<pre>
<code markup="none">
$file = fopen($file_path, "r");
while(!feof($file)) {
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
}
fclose($file);
}
else
{
die('Error: The file '.$file_name.' does not exist!');
}
</code>
</pre>
<p>That sums it up. Below is the file code.</p>
<pre>
<code markup="none">
$file_path = "/path/to/file/demosong.mp3";
$file_name = "Cool_Song.mp3";
//download speed is in kb/s
$download_rate = 50;
if(strstr($file_name,'.mp3'))
{
$download_rate = 30;
}
else
{
$download_rate = 150;
}
if(file_exists($file_path) &amp;&amp; is_file($file_path))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file_path));
header('Content-Disposition: filename='.$file_name);
flush();
$file = fopen($file_path, "r");
while(!feof($file)) {
print fread($file, round($download_rate * 1024));
flush();
sleep(1);
}
fclose($file);
}
else
{
die('Error: The file '.$file_name.' does not exist!');
}
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/post/5_basic-traffic-shaping-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
