简体   繁体   English

如何使用PHP从外部服务器复制文件?

[英]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. 我必须将JPG从外部服务器(例如Facebook)复制到我的服务器。

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. 我虽然也许用fread / fopen等。但是我不知道这是否是最好的方法。

You can use URLs with most filesystem functions (like copy ). 您可以将URL与大多数文件系统功能(例如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. 请注意,您需要启用php.ini设置allow_url_fopen

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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