exit(); is your problem. This halts all further php-execution.

The way I do this is:

the controller assembles the path to the image.
the view contains the readfile() and some header adjustments. (no
blank lines or forgotten spaces outside the php-tag)
a blank layout, also with no extra whitespace that could break the
image.

Other good tips on the subject:

I like to make the URL contain the filename, or A filename anyway just
to get an extention which helps some browsers.

http://app.site/controller/action/id/filename.jpg

Files should have a size:

header("Content-Length: " . filesize($filePath));

Some headers for making the file download:

header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=" . $filename .
";");
header("Content-Transfer-Encoding: binary");

You may also want to set some cache-headers.

If your images or other files are large you may want to save some
server-RAM by using the function readfile_chunked().
It can be found in the comments for readfile on php.net but here it is
if you want:

function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
        set_time_limit(0);
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile()
does.
   }
   return $status;

}





On Dec 17, 10:04 am, krr <[EMAIL PROTECTED]> wrote:
> A part of my application has display of images, and the images are not
> under webroot, but protected, so that only looged in users can view
> the images.
>
> In my old plain php application, php script was checking for login and
> doing a readfile($image_file_path); and I was using it as below
>
> <a href="/scripts/getimage?name=abcd.jpg">
>
> I am trying to migrate the app to cakephp, and I don't know how to
> emulate the samething in cakephp. I have written a controller for
> images (images data are stored in db table so can have a model, etc)
> and try to do the samething in getImage($imagename) method.
>
> function getImage($imagename)
> {
>     readfile($path.DS.$imagename);
>     exit();  //no view rendering needed.
>
> }
>
> It works for one image, but after the execution, I find that the
> session is no more valid. Any clue as to what is wrong or any other
> ideas to implement the same? ( I cannot copy the images under webroot
> for security purposes, not even in cache directory).

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to