我使用Curl从位置下载文件
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
header("Content-Type: application/force-download");
header("Content-disposition: attachment; filename=zipfile.tar.gz");
echo $data;
}
$rt = get_data('http://slackbuilds.org/slackbuilds/13.37/multimedia/umplayer.tar.gz');
这会下载文件,但无法打开它。是否必须提及正确的内容类型?我正在使用这个脚本的视频,所以我尝试使用Content-Type:Video,但仍然不合适。
答案 0 :(得分:2)
将CURLOPT_BINARYTRANSFER设置为TRUE并更改内容类型。
<?php
$url = "http://slackbuilds.org/slackbuilds/13.37/multimedia/umplayer.tar.gz";
$opts = array(
CURLOPT_URL =>$url,
CURLINFO_CONTENT_TYPE => "text/xml",
CURLOPT_BINARYTRANSFER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE
);
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
header("Content-Type: application/x-gzip");
header("Content-disposition: attachment; filename=zipfile.tar.gz");
echo $data;
?>
答案 1 :(得分:0)
可能你错过了header("Content-Transfer-Encoding: binary");