Alex
Alex

Reputation: 104

I want to create a controller in Yii/PHP that takes an image id and return the image

I have this Yii/PHP web app that shows a profile. I would like to create a controller that when I read user profile record from DB table, and pass the profile image id to the controller, the controller finds the picture in the user photo directory and return it instead of doing a direct link injection. For example, the <img> tag would be like this :

<img src="<?php echo 'http://www.example.com/example.php?imgid=155214' ?>" alt="photo"/>

How can I implement something like this ? I mean how do I return the image ? should I do a file read and output or is there a better way ? I appreciate your suggestion and possible code sample :)

Upvotes: 0

Views: 189

Answers (1)

sensorario
sensorario

Reputation: 21620

I suppose you have a model "Image" that stores all images. Solution can be similar to this:

class MyController extends Controller {
    public function actionMyAction($imgid) {
        $image = Image::midel()->findByPk(array(
            'condition' => 'id=:id',
            'params' => array(
                ':id' => 'id')));
        return $image->path;
    }
}

Upvotes: 1

Related Questions