This is a snippet from a project I'm working on. This serves files from a given directory and should get you quite far.
Tom
public function externalAction() {
$name = basename($this->_request->getParam('name'));
$group = basename($this->_request->getParam('group'));
$file = basename($this->_request->getParam('file'));
$file = basename($file); # for security, only use the path name below
$path = APPLICATION_PATH . "/Plugins/$group/$name/External/" . $file;
if (!file_exists($path)) $path = APPLICATION_PATH . '/../public/images/1px.gif';
header('Content-Length:' . @filesize($path));
header('Content-Type: ' . $this->get_mime_type($path));
header('Content-Disposition:inline; filename="' . basename($path) . '"');
// Give correct mime type
if ($this->config->useSendfile) {
header("X-Sendfile:" . $path);
} else {
readfile($path);
}
}
/**
* From: http://www.darian-brown.com/php-function-to-get-file-mime-type/
*
* There is certainly a better way to get this. Using php finfo_file didn't
* find text/css for .css
*/
private function get_mime_type($file)
{
// our list of mime types
$mime_types = array(
"pdf"=>"application/pdf"
,"exe"=>"application/octet-stream"
,"zip"=>"application/zip"
,"docx"=>"application/msword"
,"doc"=>"application/msword"
,"xls"=>"application/vnd.ms-excel"
,"ppt"=>"application/vnd.ms-powerpoint"
,"gif"=>"image/gif"
,"png"=>"image/png"
,"jpeg"=>"image/jpg"
,"jpg"=>"image/jpg"
,"mp3"=>"audio/mpeg"
,"wav"=>"audio/x-wav"
,"mpeg"=>"video/mpeg"
,"mpg"=>"video/mpeg"
,"mpe"=>"video/mpeg"
,"mov"=>"video/quicktime"
,"avi"=>"video/x-msvideo"
,"css"=>"text/css"
,"jsc"=>"application/javascript"
,"js"=>"application/javascript"
,"php"=>"text/html"
,"htm"=>"text/html"
,"html"=>"text/html"
);
$parts = explode('.',$file);
$end = end($parts);
$extension = strtolower($end);
// Use default
return ($mime_types[$extension]) ?: 'application/octet-stream';
}