沃梦达 / 编程问答 / php问题 / 正文

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

Need php script to download a file on a remote server and save locally(需要php脚本下载远程服务器上的文件并保存到本地)

本文介绍了需要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);

这篇关于需要php脚本下载远程服务器上的文件并保存到本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐