Remotely download a file from an external link to my server - download stops prematurely

别等时光非礼了梦想. 提交于 2019-11-28 12:54:24

This one throws an error about a failed open stream:

...
$enc = urlencode($url);
...

I'd say no wonder, because you don't need to "urlencode" that url which is already properly encoded. Try:

$url = 'http://www.example.com/file.zip';
$file = "/downloads/file.zip";
$src = fopen($url, 'r');
$dest = fopen($file, 'w');
echo stream_copy_to_stream($src, $dest) . " bytes copied.\n";

See stream_copy_to_stream­Docs. If you need to set more HTTP thingies like user-agent etc. use the HTTP Context Options­Docs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!