繁体   English   中英

PHP下载远程动态文件到本地服务器

[英]php download remote dynamic file to local server

当我对远程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;

尝试使用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");

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

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

定义什么不适用于您的代码。 您是否在PHP配置中启用了allow_url_fopen? 看看http://php.net/manual/en/features.remote-files.php

尝试这个 :

getremotetofile("http://aa.com/generatefeed.php");


    public static function getremotetofile($fileurl)
    {
      $newfilename= basename($fileurl);
        $content = file_get_contents($fileurl);
        file_put_contents($newfilename, $content);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM