Skip to content

Instantly share code, notes, and snippets.

@Artem-Schander
Last active October 15, 2015 11:26
Show Gist options
  • Save Artem-Schander/8e86d758dd5279b1ace4 to your computer and use it in GitHub Desktop.
Save Artem-Schander/8e86d758dd5279b1ace4 to your computer and use it in GitHub Desktop.
PHP Fileproxy method
<?php
/**
* @param string $file_name
* @param string $mime
* @param string/bool $download
* @return void
*/
public function fileProxyAction($file_name, $mime, $download = false) {
if(basename($file_name) != $file_name) return 'Filename not valid!';
$path = 'fileadmin/ftp.docs/';
$file = $path.$file_name;
if (!(file_exists($file) && is_readable($file))) return 'The file "'.$file_name.'" could not be found!';
// echo $file.'<br>';
// echo $mime.'<br>';
// die;
ob_clean();
if($download === false) {
header('Content-type: '.$mime);
header('Content-length: '.filesize($file));
$open = @ fopen($file, 'rb');
if ($open) {
fpassthru($open);
exit;
}
} else {
// download
$path_parts = pathinfo($file);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-type: application/octet-stream");
header("Content-length: " . filesize($file));
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Cache-control: private"); // open files directly
readfile($file);
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment