当我向远程http://aa.com/generatefeed.php
发出服务器端呼叫时它应该实时下载feed_randomnumber.csv(随机文件名)并将其保存为本地服务器。
但它不起作用。
出了什么问题。
getfeed.php
getremotetofile("http://aa.com/generatefeed.php");
public static function getremotetofile($fileurl)
{
$newfilename= basename($fileurl);
$destination=fopen($newfilename,"w");
$source=fopen($fileurl,"r");
$maxsize=3000;
$length=0;
while (($a=fread($source,1024))&&($length<$maxsize))
{
$length=$length+1024;
fwrite($destination,$a);
}
fclose($source);
fclose($destination);
}
generatefeed.php
$fullcontent="bla,bla,bla";
header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=feed_randomnumber.csv");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($strHeader));
print $fullcontent;
exit;
答案 0 :(得分:1)
尝试使用CURL而不是fopen,它可能会在您的服务器上禁用。
$file = "http://somelocation.com/somefile.php";
$ch = curl_init($file);
$fp = @fopen("temp.php", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$file = "temp.php";
$fp = fopen($file, "r");
答案 1 :(得分:0)
定义对代码不起作用的内容。你在php配置中启用了allow_url_fopen吗?看看http://php.net/manual/en/features.remote-files.php
答案 2 :(得分:0)
试试这个:
getremotetofile("http://aa.com/generatefeed.php"); public static function getremotetofile($fileurl) { $newfilename= basename($fileurl); $content = file_get_contents($fileurl); file_put_contents($newfilename, $content); }
答案 3 :(得分:0)
file_put_contents('localFile.csv', file_get_contents('http://www.somewhere.com/function.php?action=createCSVfile'));
我用它来下载动态创建的远程csv并将其保存在本地服务器上。