简体   繁体   中英

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.

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM