Search code examples
phpcakephpcakephp-2.3cakephp-2.x

Cakephp read image from app/uploads directory


I am sending images from my mobile app to the server.

I am saving the images in the directory

app/uploads/image.png

and storing the path in the database.

I want to know how can I access this image from a browser. In short, what would be the URL of this image? I want to check first in the browser before I send the path to my mobile app


Solution

  • Setting aside discussions on whether the proposed approach is the correct one or not, I'm going to focus on a providing a solution to the problem raised.

    In fact, it's quite easy. Add the following to one of your controllers:

    public function download($filename){
    
        $file = APP."/uploads/{$filename}";
        $this->response->file($file);
        return $this->response;
    }
    

    Note: The code above lacks error handling.

    You can then access

    http://www.example.com/my_controller/download/my_picture.jpg
    

    See