A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

CakePHP: Displaying images that are outside the public accessible directory (webroot) using Media Views

Let’s say that Configure::read('App.uploads_temp') is somewhere outside the public accessible directory: /var/www/yourapp/private

//This goes into the controller
    /**
     * @param $filename A filename without the path. EG: default-guy.jpg
     */
    public function downloadimage($filename) {
        $file_parts = pathinfo($filename);
        $this->viewClass = 'Media';
        $params = array(
            'id'        => $filename,
            'name'      => $file_parts['basename'],
            'download'  => false,
            'extension' => $file_parts['extension'],
            'path'      =>Configure::read('App.uploads_temp'). DS
        );
        $this->set($params);
    }

You can view the image through the URL:

    http://yourapp.com/entries/downloadimage/sample-image.jpg

Reference: http://book.cakephp.org/2.0/en/views/media-view.html

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>