使用php从URL保存文件


Save files using php from a URL

所以我有一个URL,比方说http://mysite-1.com/img.jpg,我想用PHP从另一个网站访问该URL并保存文件,我不知道从哪里开始,你们能给我一些提示吗?我正在使用CodeIgniter,我想将其与upload()方法一起使用,但任何其他方法也将受到赞赏。

一个非常非常简单的方法是:

copy("http://example.com/source.jpg","destination.jpg");

只要destination.jpg在服务器上的某个位置,它就会从远程服务器获取图像并保存,就像你想要的那样。

假设php.ini中启用了allow_url_fopen,则可以像访问本地文件一样远程访问url。

file_put_contents('/path/to/local/file', file_get_contents('http://domain.com/path/to/remote/file'));