Last active
September 2, 2015 09:55
-
-
Save putzflorian/4c3d9c3dea2414dc88a7 to your computer and use it in GitHub Desktop.
PHP Download Controller
This file contains hidden or 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 | |
| use Website\Controller\Action; | |
| use Pimcore\Model\Asset; | |
| class DownloadController extends Action { | |
| private $file; | |
| private $asset; | |
| private $filename; | |
| private $mimetype; | |
| public function init() { | |
| parent::init(); | |
| $this->removeViewRenderer(); | |
| try { | |
| $asset = Asset::getById($this->getParam("id")); | |
| if ($asset instanceof Asset) { | |
| $this->file = $asset->getFileSystemPath(); | |
| $this->filename = $asset->getFilename(); | |
| $this->mimetype = $asset->getMimetype(); | |
| $this->asset = $asset; | |
| } else { | |
| throw new \Exception($asset); | |
| } | |
| } catch (\Exception $e) { | |
| throw new \Exception($e); | |
| exit; | |
| } | |
| $this->asset = $asset; | |
| } | |
| public function downloadAction() { | |
| header('Content-type: ' . $this->mimetype); | |
| header("Content-Length: " . filesize($this->file)); | |
| header('Content-Disposition: attachment; filename="' . $this->filename . '"'); | |
| readfile($this->file); | |
| exit; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add namespaces