Php 从URL下载文件到服务器
这个看起来很简单,确实如此。要将文件下载到服务器,您只需执行以下操作:Php 从URL下载文件到服务器,php,http,stream,download,Php,Http,Stream,Download,这个看起来很简单,确实如此。要将文件下载到服务器,您只需执行以下操作: file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip")); 只有一个问题。如果您有一个大文件,比如100mb,该怎么办。然后,您将耗尽内存,无法下载该文件 我想要的是一种在下载时将文件写入磁盘的方法。这样,我就可以下载更大的文件,而不会遇到内存问题。由于PHP 5.1.0,支持通过将流句柄作为$data参数传递来逐段写入:
file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
只有一个问题。如果您有一个大文件,比如100mb,该怎么办。然后,您将耗尽内存,无法下载该文件
我想要的是一种在下载时将文件写入磁盘的方法。这样,我就可以下载更大的文件,而不会遇到内存问题。由于PHP 5.1.0,支持通过将流句柄作为$data
参数传递来逐段写入:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
从手册中:
如果数据[这是第二个参数]是流资源,则该流的剩余缓冲区将复制到指定文件。这与使用类似
(谢谢。)尝试使用cURL
set_time_limit(0); // unlimited max execution time
$options = array(
CURLOPT_FILE => '/path/to/download/the/file/to.zip',
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => 'http://remoteserver.com/path/to/big/file.zip',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
我不确定,但我相信使用CURLOPT_文件
选项,它在提取数据时写入,即未缓冲
private function downloadFile($url, $path)
{
$newfname = $path;
$file = fopen ($url, 'rb');
if ($file) {
$newf = fopen ($newfname, 'wb');
if ($newf) {
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
}
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}
.php
文件中,并在目标服务器中运行我用它来下载文件
function cURLcheckBasicFunctions()
{
if( !function_exists("curl_init") &&
!function_exists("curl_setopt") &&
!function_exists("curl_exec") &&
!function_exists("curl_close") ) return false;
else return true;
}
/*
* Returns string status information.
* Can be changed to int or bool return types.
*/
function cURLdownload($url, $file)
{
if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
$ch = curl_init();
if($ch)
{
$fp = fopen($file, "w");
if($fp)
{
if( !curl_setopt($ch, CURLOPT_URL, $url) )
{
fclose($fp); // to match fopen()
curl_close($ch); // to match curl_init()
return "FAIL: curl_setopt(CURLOPT_URL)";
}
if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";
return curl_exec($ch);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";
if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";
curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
}
// if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
// if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
// if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
if( !curl_exec($ch) ) return "FAIL: curl_exec()";
curl_close($ch);
fclose($fp);
return "SUCCESS: $file [$url]";
}
else return "FAIL: fopen()";
}
else return "FAIL: curl_init()";
}
函数cURLcheckBasicFunctions()
{
如果(!function_存在(“curl_init”)&&
!函数_存在(“curl_setopt”)&&
!函数_存在(“curl_exec”)&&
!函数_exists(“curl_close”))返回false;
否则返回true;
}
/*
*返回字符串状态信息。
*可以更改为int或bool返回类型。
*/
函数下载($url,$file)
{
如果(!cURLcheckBasicFunctions())返回“不可用:cURL基本函数”;
$ch=curl_init();
若有($ch)
{
$fp=fopen($file,“w”);
如果($fp)
{
如果(!curl_setopt($ch,CURLOPT_URL,$URL))
{
fclose($fp);//匹配fopen()
curl_close($ch);//匹配curl_init()
返回“FAIL:curl_setopt(CURLOPT_URL)”;
}
如果(!ini_get('open_basedir')&&!ini_get('safe_mode'))| |$redirects<1){
curl_setopt($ch,CURLOPT_USERAGENT,”,“Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.8.1.11)Gecko/20071204 Ubuntu/7.10(gutsy)Firefox/2.0.0.11”);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch,CURLOPT_REFERER,'http://domain.com/');
如果(!curl_setopt($ch,CURLOPT_HEADER,$CURLOPT_HEADER))返回“FAIL:curl_setopt(CURLOPT_HEADER)”;
如果(!curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$redirects>0))返回“FAIL:curl_setopt(CURLOPT_FOLLOWLOCATION)”;
如果(!curl_setopt($ch,CURLOPT_FILE,$fp))返回“FAIL:curl_setopt(CURLOPT_FILE)”;
如果(!curl_setopt($ch,CURLOPT_MAXREDIRS,$redirects))返回“FAIL:curl_setopt(CURLOPT_MAXREDIRS)”;
返回curl_exec($ch);
}否则{
curl_setopt($ch,CURLOPT_USERAGENT,”,“Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.8.1.11)Gecko/20071204 Ubuntu/7.10(gutsy)Firefox/2.0.0.11”);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch,CURLOPT_REFERER,'http://domain.com/');
如果(!curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false))返回“FAIL:curl_setopt(CURLOPT_FOLLOWLOCATION)”;
如果(!curl_setopt($ch,CURLOPT_FILE,$fp))返回“FAIL:curl_setopt(CURLOPT_FILE)”;
如果(!curl_setopt($ch,CURLOPT_HEADER,true))返回“FAIL:curl_setopt(CURLOPT_HEADER)”;
如果(!curl_setopt($ch,CURLOPT_RETURNTRANSFER,true))返回“FAIL:curl_setopt(CURLOPT_RETURNTRANSFER)”;
如果(!curl\u setopt($ch,CURLOPT\u禁止使用,false))返回“FAIL:curl\u setopt(CURLOPT\u禁止使用)”;
curl_setopt($ch,CURLOPT_USERAGENT,”,“Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.8.1.11)Gecko/20071204 Ubuntu/7.10(gutsy)Firefox/2.0.0.11”);
}
//如果(!curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true))返回“FAIL:curl_setopt(CURLOPT_FOLLOWLOCATION)”;
//如果(!curl_setopt($ch,CURLOPT_FILE,$fp))返回“FAIL:curl_setopt(CURLOPT_FILE)”;
//如果(!curl_setopt($ch,CURLOPT_HEADER,0))返回“FAIL:curl_setopt(CURLOPT_HEADER)”;
如果(!curl_exec($ch))返回“FAIL:curl_exec()”;
卷曲关闭($ch);
fclose($fp);
返回“成功:$file[$url]”;
}
否则返回“FAIL:fopen()”;
}
否则返回“FAIL:curl_init()”;
}
PHP4&5解决方案:
readfile()即使单独发送大文件,也不会出现任何内存问题。
如果已启用fopen包装器,则URL可以用作此功能的文件名
有三种方法:
缺少fopen
这对我很有用,包括本地URL:
function downloadUrlToFile($url, $outFileName)
{
if(is_file($url)) {
copy($url, $outFileName);
} else {
$options = array(
CURLOPT_FILE => fopen($outFileName, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
}
在php中使用一个简单的方法
copy()
注意:如果目标文件已经存在,它将被覆盖
注意:您需要为目标文件夹设置权限777。
下载到本地计算机时使用此方法
特别注意:777是基于Unix的系统中的一种权限,拥有对所有者、组和所有人的完全读/写/执行权限。通常,我们将此权限授予不需要在web服务器上对公众隐藏的资产。例如:images文件夹。简单解决方案:
<?php
exec('wget http://someurl/file.zip');
最佳解决方案
在系统中安装aria2c&
这是在您的服务器配置中设置的,据我所知,PHP无法真正绕过它(除了直接的.ini编辑)通常情况下,这很好,但我在web应用程序中有此代码,因此我不能确定用户是否安装了cURL。但是,我对此进行了投票。@Geoff这是一个分布式web应用程序吗?因为如果你控制主机,那么你的用户就无关紧要了(cURL是你服务器上的一个库)不。我不控制托管。这是一个任何人都可以拥有的分布式web应用。Curl可能缺失。但几乎所有共享托管公司都安装了Curl b
function downloadUrlToFile($url, $outFileName)
{
if(is_file($url)) {
copy($url, $outFileName);
} else {
$options = array(
CURLOPT_FILE => fopen($outFileName, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
}
copy($source_url, $local_path_with_file_name);
<?php
exec('wget http://someurl/file.zip');
echo exec("aria2c \"$url\"")