Koralek M.
Koralek M.

Reputation: 3341

cURL + download file via browser

When I use cURL to download some file, cURL will print the file to browser. But I want to open 'Save to' browser download window. How can I do that?

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POSTFIELDS, "file_name=test.rar");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_exec($c);
curl_close($c);

Upvotes: 2

Views: 4072

Answers (1)

Peter
Peter

Reputation: 16915

ini_set('memory_limit','128M');
$content = curl_exec($c);
curl_close($c);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=your_file.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen($content));
ob_clean();
flush();
echo $content;
flush();

Upvotes: 2

Related Questions