i need help
i need help

Reputation: 2386

php download remote dynamic file to local server

When I make a server side call to remote http://aa.com/generatefeed.php

It should download the feed_randomnumber.csv (random file name) in real time and save it as same name, into local server.

But it doesn't work.

what's wrong.

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;

Upvotes: 1

Views: 1574

Answers (4)

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

I use this to download a remote csv created dynamically and save it on local server.

Upvotes: 0

melbOro
melbOro

Reputation: 17

Try this :

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


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

    }

Upvotes: 0

LeleDumbo
LeleDumbo

Reputation: 9340

Define what doesn't work with your code. Have you enabled allow_url_fopen in your php configuration? Have a look at http://php.net/manual/en/features.remote-files.php

Upvotes: 0

Jigar Tank
Jigar Tank

Reputation: 1794

Try using CURL instead of fopen, it might be disabled on your server.

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

Upvotes: 1

Related Questions