Posts Tagged ‘ php

PHP-Zend 1: create an ImageController

During the development of a website I encountered the following problem: how to load some images located outside the document root into a ZF1 view? Obviously we could move these images into the document root but just to be sure we decided to leave them outside (these images are created with an external script), and in that place it’s impossible to get them with the standard approach ( <img src="relative/path">¬†).

Now I found, thanks to a hint from a senior colleague, a way to overcome this problem: create a Zend Framework (we used the version 1.12.3 in this project) controller that has only to load the image and create a response with only the image within. By using a ZF controller you’ll be able to check if the user has the permission to open the image with a few queries and by using the current user session.

Now the only thing to do is to call this controller with a parameter: in the project I used the id of the image because the path is saved into a mysql table, but obviously you can use what you need.

Here comes the self-explanatory snippet of this controller:


Now when you want to load this image you only have to write a code like this:

return top