2011-11-14 93 views
1

当我做一个服务器端调用远程http://aa.com/generatefeed.phpPHP下载远程动态文件下载到本地服务器

应该下载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; 

回答

1

使用替代的fopen CURL尝试,它可能会在服务器上被禁用。

$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");

+0

此方法可以正常工作,但复制过来的csv文件只有1个标题行,所有其他行无法捕获 –

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); 

    }
0
file_put_contents('localFile.csv', file_get_contents('http://www.somewhere.com/function.php?action=createCSVfile')); 

我用它来下载动态创建的远程CSV并将其保存在本地服务器上。

相关问题