php 需要php脚本下载远程服务器上的文件并保存到本地
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4504212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need php script to download a file on a remote server and save locally
提问by bigLarry
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.
以下代码似乎适用于小于 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);
?>
Any suggestions on how to download larger files without interruption?
关于如何不间断地下载较大文件的任何建议?
回答by Parris Varney
$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);
回答by Kuldeep
Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:
自 PHP 5.1.0 起,file_put_contents() 支持通过将流句柄作为 $data 参数传递来逐段写入:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
回答by Blagovest Buyukliev
file_get_contents
shouldn't be used for big binary files because you can easily hit PHP's memory limit. I would exec()
wget
by telling it the URL and the desired output filename:
file_get_contents
不应用于大型二进制文件,因为您很容易达到 PHP 的内存限制。我会exec()
wget
告诉它 URL 和所需的输出文件名:
exec("wget $url -O $filename");
回答by Hbirjand
I always use this code,it's working very well.
我总是使用此代码,它运行良好。
<?php
define('BUFSIZ', 4095);
$url = 'Type The URL Of The File';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>
回答by Salar
Use this solution if you do not know the format of the file that you are going to download.
如果您不知道要下载的文件的格式,请使用此解决方案。
$url = 'http:://www.sth.com/some_name.format' ;
$parse_url = parse_url($url) ;
$path_info = pathinfo($parse_url['path']) ;
$file_extension = $path_info['extension'] ;
$save_path = 'any/local/path/' ;
$file_name = 'name' . "." . $file_extension ;
file_put_contents($save_path . $file_name , fopen($url, 'r'))
回答by Saeed Sepehr
A better and lighter script which is streaming file:
一个更好更轻的流文件脚本:
<?php
$url = 'http://example.com/file.zip'; //Source absolute URL
$path = 'file.zip'; //Patch & file name to save in destination (currently beside of PHP script file)
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
回答by aditya4447
Try out phpRFT:http://sourceforge.net/projects/phprft/files/latest/download?source=navbar
试试 phpRFT:http: //sourceforge.net/projects/phprft/files/latest/download?source =navbar
It have progress_bar and simple filename detactor...
它有progress_bar 和简单的文件名检测器...