[英]PHP cURL download does not work when called from browser
I want a php script which I can access from browser and that script will download files to my remote server (not to my computer). 我想要一个可以从浏览器访问的php脚本,该脚本会将文件下载到我的远程服务器(而不是我的计算机)。 This is complete code which I call from browser as localhost/download.php but it does not work (in network manager I see that something is being downloaded but in project folder I can not find it):
这是完整的代码,我从浏览器中将其称为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);
?>
But it works when I call below command from command line: 但是当我从命令行调用以下命令时,它可以工作:
php /var/www/idym/download.php
What is the problem? 问题是什么?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
the difference might be a result of different config for cli php and apache php. 差异可能是由于cli php和apache php配置不同的结果。
http://php.net/manual/en/function.curl-setopt.php http://php.net/manual/zh/function.curl-setopt.php
complete code: 完整的代码:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.