Pierre Lebon
Pierre Lebon

Reputation: 463

How do I copy a file from an external server with PHP?

I have to copy JPG from an external server, such as Facebook, to my server.

What is the best way to do so? I though maybe with fread/fopen etc. but I don't know if it is the best way.

Upvotes: 1

Views: 3942

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16563

You can use URLs with most filesystem functions (like copy).

$url = "http://cdn.sstatic.net/stackoverflow/img/sprites.png";
$target = "/tmp/stackoverflow.png";
copy($url, $target);

Do note that you need to have the php.ini setting allow_url_fopen enabled.

Upvotes: 3

Husman
Husman

Reputation: 6909

 $url = 'http://facebook.com/image.jpg';

 $img = '/my/folder/image.jpg';

 file_put_contents($img, file_get_contents($url));

Upvotes: 1

Related Questions