php to download an image onto server!

Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
http://snoopy.sourceforge.net/

Code:
require_once 'snoopy.php';

$snoopy = new snoopy();

$snoopy->fetch('http://site.com/image.png');
$image = $snoopy->results;

$fh = fopen('image.png', 'w');
fwrite($fh, $image);
fclose($fh);

Writes site.com/image.png to ./image.png.

Edit: nevermind. With allow_url_fopen on:

PHP5:
Code:
file_put_contents('image.png', file_get_contents('http://site.com/image.png'));

PHP4:
Code:
$fh = fopen('image.png', 'w');
fwrite($fh, file_get_contents('http://site.com/image.png'));
fclose($fh);
 
Soldato
Joined
11 May 2004
Posts
4,790
Location
Gloucester
This code will take a URL via the query string, download it and write it to a file called "imagefile".

If you want to change the image at all, you'll need to use the GD functions. You can also use them to verify it really is an image, and not a renamed file of some other kind.

[edit] Rob replied before me, this is essentially the same code but with error checking.

Code:
<?php
    if (isset($_GET['url']) && eregi("^(http://)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?((([12]?[0-9]{1,2}\.){3}[12]?[0-9]{1,2})|(([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.(com|net|org|edu|mil|gov|int|aero|coop|museum|name|info|biz|pro|[a-z]{2})))(:[1-6]?[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+(\.jpg|\.gif|\.png|\.jpeg)$", $_GET['url'])) {
        $image = file_get_contents($_GET['url']);
        if (!$image) {
            die("Unable to fetch URL");
        }
        $f = fopen("imagefile", "w");
        if (!$f) {
            die("Unable to open file for writing");
        }
        fwrite($f, $image);
        fclose($f);
    } else {
        die("Invalid Image URL");
    }
?>
 
Man of Honour
Joined
31 Jan 2004
Posts
16,335
Location
Plymouth
Why not just preg_match() (or ereg-whatever) the characters after the last full stop (ie the extension), rather than try and regex the whole URL....that one currently cuts out a whole bunch of the intarweb (.co.uk, .whatever.uk, and lottttttts of others) :)
 
Associate
Joined
5 Jun 2004
Posts
515
Location
Cambridge
Beansprout said:
Why not just preg_match() (or ereg-whatever) the characters after the last full stop (ie the extension), rather than try and regex the whole URL....that one currently cuts out a whole bunch of the intarweb (.co.uk, .whatever.uk, and lottttttts of others) :)

No it don't:

Code:
...(com|net|org|edu|mil|gov|int|aero|coop|museum|name|info|biz|pro|[b][a-z]{2}[/b])...

But I totally agree that it does seem a bit OTT.


EDIT: Actually, I think I'm being a right tit, Beansprout probably is right. :o

<--- Needs more sleep...
 
Last edited:
Man of Honour
Joined
31 Jan 2004
Posts
16,335
Location
Plymouth
I got a bit lost in that huuuge regexp so I could be wrong. Either way I'd just do something like this...

Code:
preg_match("/\.(jp(e?)g|gif|png|(w?)bmp)$/i",$url);

Regular expressions are totally my antichrist though :)

All this is probably academic since the image could be dynamic, and you should use something like getimagesize() to get the image's mime type, too.
 
Back
Top Bottom