Last active
July 17, 2017 00:53
-
-
Save jakubboucek/9163570425ce25e706328d6a9c6d2ff3 to your computer and use it in GitHub Desktop.
Self-downloadable package maker inPHP
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
<?php | |
/** Create PHP file with self-downloadable content regardless to file-type and server settings | |
@var $source Full filname of file to download | |
@var $target Path to save self-downloadable package | |
@return Package info | |
*/ | |
function createDownloadablePackage($source, $target) { | |
$filename = basename($source); | |
$filesize = filesize($source); | |
$dest = $target."/cache_".md5(mt_rand()).".php"; | |
$fp = fopen($source, "r"); | |
$fo = fopen($dest, "w"); | |
fwrite($fo, getHeader($filename, $filesize)); | |
stream_copy_to_stream($fp, $fo); | |
$response = [ | |
'filename' => $filename, | |
'filesize' => $filesize, | |
'dest' => $dest, | |
]; | |
return $response; | |
} | |
function getHeader($filename, $filesize) { | |
$header = "<"."?php " . | |
"\$fp = fopen(__FILE__, \"r\");" . | |
"\$fo = fopen(\"php://output\", \"w\");" . | |
"header(\"Content-Type: application/octet-stream\");" . | |
"header(\"Content-Length: $filesize\");" . | |
"header(\"Content-Disposition: attachment; filename=\\\"$filename\\\"\");" . | |
"fseek(\$fp, __COMPILER_HALT_OFFSET__);" . | |
"stream_copy_to_stream(\$fp, \$fo);" . | |
"__halt_compiler();"; | |
return $header; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment