我想要一个php脚本,我可以从浏览器访问,该脚本将下载文件到我的远程服务器(不是我的计算机)。这是完整的代码,我从浏览器调用localhost/download.php,但它不起作用(在网络管理器中,我看到正在下载的东西,但在项目文件夹中我找不到它):
<?
function download($url, $referer, $name)
{
$path = $name.'.mp3';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.5) Gecko/20100101 Firefox/13.4.9'r'n',
"Referer: $referer"
));
$data = curl_exec($ch);
curl_close($ch);
fwrite($fp, $data);
fclose($fp);
}
echo 'Please wait it is downloading...<br/>';
/*$url = $_GET['url'];
$referer = $_GET['referer'];
$name = $_GET['name'];*/
$url = "http://stream.get-tune.net/file/91259796/101538464/3652118538/ee58ddeb905ffa12/Palwan_Halmyradow_-_Sensiz_(get-tune.net).mp3";
$referer = "http://get-tune.net/?a=music&q=palwan+halmyradow";
$name = "Palwan Halmyradow - Sensiz";
download($url, $referer, $name);
?>
但是当我从命令行调用下面的命令时,它可以工作:
php /var/www/idym/download.php
有什么问题吗?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
差异可能是cli PHP和apache PHP配置不同的结果。
http://php.net/manual/en/function.curl-setopt.php完整代码:
function download($url, $referer, $name)
{
$path = $name.'.mp3';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.5) Gecko/20100101 Firefox/13.4.9'r'n',
"Referer: $referer"
));
$data = curl_exec($ch);
curl_close($ch);
fwrite($fp, $data);
fclose($fp);
}