I am currently developing an application in PHP in which my server (a dedicated server) must to download a file, and the user should download the file in same time.
Here is an example :
- Server start to download a file at a time A.
- User wants to download this file at the time A + 3 seconds (for example)
I already solved the problem :”If the user downloads the file faster than the server..“. But I didn’t know how to make a php script in which the user is gonna to download the full file (it means that the size must be the full size of the file, not the size it’s currently downloaded at the time A+3seconds). I already make that :
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$data['name'].'";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$data['size']);
readfile($remoteFile);
But it doesn’t work, the user is gonna download just the size it is currently on the server (which corrupt the file) and not the full file…
If you have any solution, thank you.
You could probably pipe the file manually, by opening the connection and reading until you’re past all headers. Then once you’ve figured out the
Content-Length, send that to the user and justechoall remaining data you get (do useflush()and avoid output buffers).Pseudocode(-ish):