<?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>HumanUmbrella.com &#187; save remote file</title>
	<atom:link href="http://www.humanumbrella.com/tag/save-remote-file/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.humanumbrella.com</link>
	<description>A struggle for clarity in life; searching beyond barriers between us.</description>
	<lastBuildDate>Mon, 17 May 2010 04:09:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to download a remote file in php and then save it</title>
		<link>http://www.humanumbrella.com/2007/12/08/how-to-download-a-remote-file-in-php-and-then-save-it/</link>
		<comments>http://www.humanumbrella.com/2007/12/08/how-to-download-a-remote-file-in-php-and-then-save-it/#comments</comments>
		<pubDate>Sat, 08 Dec 2007 23:45:45 +0000</pubDate>
		<dc:creator>humanumbrella</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[My Linux Journey]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[in php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[remote file]]></category>
		<category><![CDATA[remote image]]></category>
		<category><![CDATA[save remote file]]></category>
		<category><![CDATA[save remote image]]></category>

		<guid isPermaLink="false">http://www.humanumbrella.com/?p=27</guid>
		<description><![CDATA[So, I found a site with all the lolcat images named cat1.jpg, cat2.jpg, cat3.jpg, etc.  I wanted to know how many were there as I saw cat389.jpg.  As it turns out, it&#8217;s only 390, so that&#8217;s all I need to know.
So, I decided to write a little PHP script to grab all the [...]]]></description>
			<content:encoded><![CDATA[<p>So, I found a site with all the lolcat images named cat1.jpg, cat2.jpg, cat3.jpg, etc.  I wanted to know how many were there as I saw cat389.jpg.  As it turns out, it&#8217;s only 390, so that&#8217;s all I need to know.</p>
<p>So, I decided to write a little PHP script to grab all the images.  First, I set up a loop to do this, from 1-390.  which is done like<br />
<span id="more-27"></span></p>
<blockquote><p><code>for($i=1;$i&lt;391;$i++){</code></p></blockquote>
<p>Then, I get the data from the URL.  By putting &#8220;.$i.&#8221; in the filename string, I can increment the filename as the loop increments. so it&#8217;ll get filename1.jpg, filename2.jpg, etc&#8230;</p>
<blockquote><p><code>$x = http_get_file('http://the_site/filename'.$i.'.jpg');</code></p></blockquote>
<p>This is a method call, which I&#8217;ll post next.</p>
<blockquote><p><code>function http_get_file($url)  {<br />
$url_stuff = parse_url($url);<br />
$port = isset($url_stuff['port']) ? $url_stuff['port']:80;<br />
$fp = fsockopen($url_stuff['host'], $port);<br />
$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";<br />
$query .= 'Host: ' . $url_stuff['host'];<br />
$query .= "\n\n";<br />
fwrite($fp, $query);<br />
while ($line = fread($fp, 1024)) {<br />
$buffer .= $line;<br />
}<br />
preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);<br />
return substr($buffer, - $parts[1]);<br />
}</code></p></blockquote>
<p>Got it from -&gt; <a href="http://damonparker.org/blog/2005/09/29/download-a-remote-file-using-php/" title="download file in php">this site</a> (Thanks!)</p>
<p>Finally, you write what you get back from this method ( a buffer of data ) to a file, which in PHP is done like this:  (again, &#8220;cat&#8221;.$i.&#8221;.jpg&#8221; creates cat1.jpg,cat2.jpg for whatever i is at the time.)</p>
<blockquote><p><code>$File = "cat".$i.".jpg";<br />
$Handle = fopen($File, 'w');<br />
$Data = $x;<br />
fwrite($Handle, $Data);<br />
print "Data Written";<br />
fclose($Handle);</code></p></blockquote>
<p>All you have to do is make sure that the directory your script is in has the ability to create/write files.</p>
<p>That&#8217;s it!</p>
<p>Enjoy!</p>
<p><strong>edit:</strong> My style scripting messes with the quotes in the script, so I&#8217;ve attached it as a standalone file to correct for this.</p>
<p>Here&#8217;s the code all together:<br />
<a href="http://www.humanumbrella.com/wp-content/uploads/2007/12/remote-file-download1.txt" title="How to download a remote file in php and then save it">How to download a remote file in php and then save it</a></p>
<p>Rename the file to remote-file-download.php after you download it.</p>
<p>Cheers~</p>
]]></content:encoded>
			<wfw:commentRss>http://www.humanumbrella.com/2007/12/08/how-to-download-a-remote-file-in-php-and-then-save-it/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
