Skip to content

Instantly share code, notes, and snippets.

@putzflorian
Last active September 2, 2015 09:55
Show Gist options
  • Select an option

  • Save putzflorian/4c3d9c3dea2414dc88a7 to your computer and use it in GitHub Desktop.

Select an option

Save putzflorian/4c3d9c3dea2414dc88a7 to your computer and use it in GitHub Desktop.
PHP Download Controller
<?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;
}
}
@putzflorian

Copy link
Copy Markdown
Author

add namespaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment