0

This code i wrote does work and downloads file to client browser but with a delay, because it first downloads file to a some kind of temp directory maybe and then when thats done it just passes it to client. I don't want this i want file to be downloaded without any delay.

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.filesize');

$chdownload = curl_init();

curl_setopt($ch, CURLOPT_URL, $url]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
curl_close($ch);


echo $data;
vragolan
  • 1
  • 2

1 Answers1

1

The file is not downloaded to a temporary disk location. The delay that you're describing is caused by the fact that curl_exec() is blocking your script's execution as it needs to wait until the entire file is downloaded before your script can continue to run.

To get access to the data while it's being downloaded, you'll need to use streams with curl (using CURLOPT_FILE) for older curl versions or use CURLOPT_WRITEFUNCTION with a callback function for newer versions of curl. Check out this answer for a detailed solution.

Community
  • 1
  • 1
dcro
  • 13,294
  • 4
  • 66
  • 75