Last active
October 5, 2021 22:44
-
-
Save rpowis/4667115 to your computer and use it in GitHub Desktop.
Check for remote file and force the user to download if it exists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$file = 'http://www.example.com/path/to/file.ext'; | |
$headers = array_change_key_case(get_headers($file, TRUE)); | |
$filesize = $headers['content-length'][1]; | |
if ( $headers[0] != 'HTTP/1.1 404 Not Found' ) { | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename='.basename($file)); | |
header('Content-Transfer-Encoding: binary'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . $filesize); | |
echo "downloading"; | |
ob_clean(); | |
flush(); | |
readfile($file); | |
exit; | |
} | |
while(1) { | |
Echo "\n"; | |
if ( connection_status() != 0 ) { | |
die; | |
} | |
} |
This works but I had to replace $filesize = $headers['content-length'][1];
with $filesize = $headers['content-length'];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a way it download file in chunk and deliver in chunk? so no memory overflow?