Request and Response on Images break occasionally

I have a weird problem and i hope you can point me in the right direction in solving it. Hopefully I can explain it well.

Okay, i have these images that I don’t want the public to see without proper authorization. In this case, I’m using an authentication login that i built, using acos/aros. If a person is logged in, they go to a page and see all the images. They click on the image and it gets bigger.

When the images were on webroot/images, I found that anyone with the link can see them, regardless if they were logged in or not.

Researching Cake, I found that I should take them OUT of webroot, which I did and put them under app/data/cutplots.

Now I just need to set a response type and it should work I thought.

my link looks something like this

<a href="/website/plots/show_cutplot/7/20151119_060006_060016_3D_MMS4_HPCA_He2_Vy_Vz_FLUX_S.png"> <img src="/website/plots/show_cutplot/7/20151119_060006_060016_3D_MMS4_HPCA_He2_Vy_Vz_FLUX_S.png"> </a>

In my PlotsController, i have a function called show_cutplot that takes 2 variables. an ID, and the filename of the image

on my backend, the pictures are located in users’ folders. so Configure Read cutplots is Configure::write(‘cutplots’, APP.‘data/cutplots/’);

an example image would be in app/data/cutplots/USERNAME/ID/filename.png

function show_cutplot ($id, $filename)
    $dir = $this->Auth->user ('username').DS.$id.'/';
    $this->response->type(array('png' => 'image/png'));
    $this->response->file(Configure::read ('cutplots').$dir.$filename);
    if ($this->response->checkNotModified($this->request)) {
        return $this->response;
    $this->layout = 'ajax';
    $this->autoRender = false;
    return $this->response;

Now, this works! the images show up, and if i’m logged out of the site and try to call that link, i get redirected to the homepage.

Now to my problem:

Sometimes it DOESN’T work and some of the images break. When I refresh the page, it logs me out and says I’m unauthorized. Sometimes i can refresh the page and all the images come right up, and everything is fine.

My images never broke when i had them on webroot/images, so I’m assuming i’m using the response incorrectly.

Any help is appreciated.

Looks to me that the images fail to load when you’re no longer logged in (session timed out) since when you refresh the page you’re redirected to the login page.

Also these lines are not necessary:
$this->layout = 'ajax' $this->autoRender = false
as you can see in this example.

1 Like

I don’t get any errors, so I dunno what would cause the site to log me out.

Also, i took out those lines and oddly, it seems to be working better. I keep refreshing and everything is coming up. thanks! Hopefully that was it.