Download Remote File to Server with PHP

2019-01-18 06:56发布

I've been looking all over the place for the last two days and trying everything and still can't get anything to work. I feel like this should be a relatively simple thing to do.

All I want to do is download a remote file from a URL to a directory on my server.

So, for example, if

$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk

and $_dir = /www/downloads/

Then when all is said and done I want 1306495040_Number_Blink_1.1.1.apk in /www/downloads/

I've tried the copy() function, I've tried

file_put_contents("$_dir.$_file_name", file_get_contents($_url));

and get the following error:

file_get_contents(): failed to open stream: HTTP request failed!

7条回答
爷的心禁止访问
2楼-- · 2019-01-18 07:45
$url  = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);

curl_close($ch);

file_put_contents($path, $data);

it uses curl

$url is the file url

$path is where and the name to save the file

i hope it works

查看更多
登录 后发表回答