How to download a remote file in php and then save it

December 8, 2007 by humanumbrella Leave a reply »

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’s only 390, so that’s all I need to know.

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

for($i=1;$i<391;$i++){

Then, I get the data from the URL. By putting “.$i.” in the filename string, I can increment the filename as the loop increments. so it’ll get filename1.jpg, filename2.jpg, etc…

$x = http_get_file('http://the_site/filename'.$i.'.jpg');

This is a method call, which I’ll post next.

function http_get_file($url) {
$url_stuff = parse_url($url);
$port = isset($url_stuff['port']) ? $url_stuff['port']:80;
$fp = fsockopen($url_stuff['host'], $port);
$query = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
$query .= 'Host: ' . $url_stuff['host'];
$query .= "\n\n";
fwrite($fp, $query);
while ($line = fread($fp, 1024)) {
$buffer .= $line;
}
preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
return substr($buffer, - $parts[1]);
}

Got it from -> this site (Thanks!)

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, “cat”.$i.”.jpg” creates cat1.jpg,cat2.jpg for whatever i is at the time.)

$File = "cat".$i.".jpg";
$Handle = fopen($File, 'w');
$Data = $x;
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);

All you have to do is make sure that the directory your script is in has the ability to create/write files.

That’s it!

Enjoy!

edit: My style scripting messes with the quotes in the script, so I’ve attached it as a standalone file to correct for this.

Here’s the code all together:
How to download a remote file in php and then save it

Rename the file to remote-file-download.php after you download it.

Cheers~

Advertisement

14 Responses

  1. LarcenIII says:

    Ok, I’ve been working with this for days and can’t get it to work, nor do I get any errors. What should my premission be set to? currently the php file and folder are 755

  2. Hey LarcenIII,

    First, let’s turn on error reporting in the PHP script, so that it will tell us what is wrong, hopefully. This is not enabled by default, which is a little bit of a problem in debugging purposes.
    Put this in the top of your code:

    error_reporting(E_ALL);

    The folder where you want to drop the images should have write-permission for group and other. I have mine at 777. If you don’t like that, you can just do it while you run the script and then change it back afterwards.

    Post back if you still don’t get it. You can e-mail me your code at humanumbrella@gmail.com and I’ll take a look – just do it before I go back to school (while I have free time) January 2nd.

    Good Luck!
    –Justin

  3. LarcenIII says:

    Ok I figured it out, I re typed it all over by hand and there’s something wrong with whats on your page. I believe you backward quotes on your page instead of regular quotes….

  4. That’s what it is, for some reason when I put the code up for download it changed the single quotes ‘ to another version of the quote whose ASCII code I can’t remember.
    Glad you got it working, I’ll attach the code for others instead of pasting it.
    See you around~
    –Justin

  5. LarcenIII says:

    Ok I figured it out, I re typed it all over by hand and there’s something wrong with whats on your page. I believe you backward quotes on your page instead of regular quotes….

    example:

    $query .= ā€œ\n\nā€;
    Those quotes aren’t right. Its like open quotes and close quotes. I don’t even know how to make those kinds of quotes.

  6. Yea, Thanks for that LarcenIII; My stylesheet for the site is putting a ’spin’ on double and single quotes, I’ll try to fix it, but the code in the text file will work.
    edit: There we go. It appears that if I wrap the code in blockquote tags the stylesheet leaves it alone. Thanks for your help! Hope you enjoy the site~

  7. LarcenIII says:

    I see that! And yes, thanx for the site! I’ll reference your page whenever possible.

    ++LarcenIII

  8. affiq says:

    thanks for sharing this script..hopefully it can helps me to finish up my final project :)

  9. joomservices says:

    Thanks for your solution. It said my time .

  10. Peter U says:

    To follow up LarcenIII’s comment, I think what happened is that at some step along the way, the code was pasted into MS word or some other rich text editor. Once I changed all the quotes, it worked like a charm though. Thanks!

  11. Yes, Peter. For some reason, the style scripting I have set up on my blog changes ” and ‘ into weird quotes.

    I posted that in my response — there is a plain text version linked at the end of the article which has the entire code.

    Cheers!
    –Justin

  12. procyon says:

    pretty good tutorial mate, was very handy for something i had to make that was breaking for wrongly streaming the remote file. seemed like it sometimes didn’t gave the stream like it should, just “lost” some pieces or something.
    this way, i simply download the file analyse it, do the treatment i need to and then simply delete the file. :P

    well, thanks mate. :)

Leave a Reply