需要php脚本来在远程服务器上下载文件并在本地保存 [英] Need php script to download a file on a remote server and save locally

查看:711
本文介绍了需要php脚本来在远程服务器上下载文件并在本地保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下代码似乎适用于小文件, 1MB,但较大的文件只是超时,甚至没有开始下载。

 <?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);

?>任何有关如何下载较大文件而不中断的建议?


$ b class =h2_lin>解决方案

  $ 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);


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

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脚本来在远程服务器上下载文件并在本地保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆