Skip to content

Instantly share code, notes, and snippets.

@VAdaihiep
Created April 20, 2018 08:14
Show Gist options
  • Save VAdaihiep/e1da6107ca1d4f9bbb6ce7fade53c12b to your computer and use it in GitHub Desktop.
Save VAdaihiep/e1da6107ca1d4f9bbb6ce7fade53c12b to your computer and use it in GitHub Desktop.
PHP Controller view file or download file. Use to check permission before return file like attach file in topic forum, private message,etc. Avoid user public file url. Ex: To view image: localhost/image.php?mode=view. To download image: localhost/image.php?mode=download.
// Check permission,...
// View image or download
$filePath = '/data/.../demo-image.jpg';
$name = basename($filePath);
if($params['mode'] == 'view'){
header("Content-Type: " . mime_content_type($filePath));
header('Content-disposition: inline; filename="' . $name . '"');
} else {
header("Content-Type: application/octet-stream");
header('Content-Length: ' . filesize($filePath));
header('Content-disposition: attachment; filename="' . $name . '"');
}
readfile($filePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment