Skip to content

Instantly share code, notes, and snippets.

@onubadev
Created March 15, 2017 14:03
Show Gist options
  • Save onubadev/122dc350daf5970345bc50f8c3cdfd37 to your computer and use it in GitHub Desktop.
Save onubadev/122dc350daf5970345bc50f8c3cdfd37 to your computer and use it in GitHub Desktop.
PHP Force file download
<?php
//Visto en: http://ecapy.com/category/php/index.html
/**
* Downloader
*
* @param $archivo
* path al archivo
* @param $downloadfilename
* (null|string) el nombre que queres usar para el archivo que se va a descargar.
* (si no lo especificas usa el nombre actual del archivo)
*
* @return file stream
*/
function download_file($archivo, $downloadfilename = null) {
if (file_exists($archivo)) {
$downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $downloadfilename);
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: ' . filesize($archivo));
ob_clean();
flush();
readfile($archivo);
exit;
}
}
/*
Uso:
download_file("archivos/archivoReal.zip", "archivoEnmascarado.zip");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment