需要php脚本下载远程服务器上的文件并保存到本地

2022-01-02 00:00:00 download php

尝试在远程服务器上下载文件并将其保存到本地子目录.

Trying to download a file on a remote server and save it to a local subdirectory.

以下代码似乎适用于小文件,<1MB,但较大的文件会超时,甚至无法开始下载.

The following code seems to work for small files, < 1MB, but larger files just time out and don't even begin to download.

<?php

 $source = "http://someurl.com/afile.zip";
 $destination = "/asubfolder/afile.zip";

 $data = file_get_contents($source);
 $file = fopen($destination, "w+");
 fputs($file, $data);
 fclose($file);

?>

关于如何不间断地下载较大文件的任何建议?

Any suggestions on how to download larger files without interruption?

推荐答案

$ch = curl_init();
$source = "http://someurl.com/afile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/asubfolder/afile.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

相关文章