Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从URL保存文件_Php_Codeigniter - Fatal编程技术网

使用php从URL保存文件

使用php从URL保存文件,php,codeigniter,Php,Codeigniter,所以我有一个URL,比如说http://mysite-1.com/img.jpg 我想从另一个网站用PHP访问这个URL并保存文件,我不知道从哪里开始,你们能给我一些提示吗?我使用的是CodeIgniter,我想将其与上载方法一起使用,但任何其他方法也将受到赞赏。假设在php.ini中启用了allow\u url\u fopen,您可以像访问本地文件一样远程访问url file_put_contents('/path/to/local/file', file_get_contents('http

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

假设在php.ini中启用了allow\u url\u fopen,您可以像访问本地文件一样远程访问url

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

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

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

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

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

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

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

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

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

是的,使用copy可能更好。是的,使用copy可能更好。+1,回答很好,不知道copy可以与URL一起工作,需要文件获取内容/文件放置内容构造。谢谢,它可以工作。非常简单的方法,出色的结果。+1,回答很好,不知道复制可以与URL一起工作,需要文件\u get\u contents/file\u put\u contents构造。谢谢,它可以工作。方法确实简单,效果显著。