Created
April 20, 2018 08:14
-
-
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.
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
// 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