tags:

views:

53

answers:

3

Hi,

is there a function in php which allows to download external file from another server, and put it in mine ?

Thank you

+5  A: 

If it's activated on your web space, you can use

file_get_contents()

If you need to simulate a user-agent, do a log-in or other advanced stuff when downloading, you may want to look into the curl family of functions.

Hard core programmers who do it themselves use fsockopen() and consorts to build a connection from ground up.

Pekka
Thanks , i'm looking for a function which reads/ and write , thanks so much
axel gold
curl can handle file uploads, too, and is probably your best bet. Check out http://stackoverflow.com/questions/1692186/image-upload-using-php-curl
Pekka
when i try to fwrite the image to my server, it displays error fail to access /tmp/new.jpg even my TMP directory is set to 777
axel gold
+1  A: 

you can use fopen/fread

function download($src, $dst) {
        $f = fopen($src, 'rb');
        $o = fopen($dst, 'wb');
        while (!feof($f)) {
            if (fwrite($o, fread($f, 2048)) === FALSE) {
                   return 1;
            }
        }
        fclose($f);
        fclose($o);
        return 0;
}
download("http://www.somewhere/image.jpg","test.jpg");
ghostdog74
A: 

There isn't a function that will read and write a remote file to a new location at the same time. That wouldn't be very useful in most cases. It's pretty easy to accomplish with a pair of functions though:

file_put_contents("local-file.jpg",file_get_contents("http://x.com/img.jpg"));
zombat
Ah, *that's* what the OP meant by read and write. Ah well...
Pekka