Hi mona,
It is not too hard to display "dynamic" images, protected images or in
your case database-stored images.

You need two parts for this puzzle.

1: Your controller needs a new method called "image" or something like
it.
This method will pretty much be a copy of your view method.
This method (it's view) needs to output a binary image.
You gan get fancy with MediaView and things like that but start by
setting some headers and just ehoing out the image data.

You shouldn't need all these but these are the headers I send for
dynamic images... seems to work the best in all browsers with all
these.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Type: $filetype");
header("Accept-Ranges: bytes");
header("Content-Disposition: inline;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " .$filesize);


2: Your current view needs to point to the method above for the
relevant image tag. Some browsers seem to almost require an extension
on an image so you may want to add it in the view and parse it out in
the controller even if you don't use "parseExtensions".

http://app.site/entries/image/23
http://app.site/entries/image/23.jpg

or if you get really fancy and use parseExtensions
http://app.site/entries/view/23.jpg





On Jan 12, 3:14 pm, mona <[email protected]> wrote:
> i m storing images in database and i wan't to display images in my
> view how to do this in cake php
>
> my controller code is
> <?php
> class EntriesController extends AppController
>  {
>         var $name = 'Entries';
>     var $helpers = array('Html','Form','Javascript','Ajax');
>         //var $uses=array('Entry','User');
>     var $components = array('RequestHandler');
>         function index(){
>         //$count = $this->Entry->find('all', array('fields' => array('(max
> (Entry.counter)) as max_counter')));
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>         $count=$row[0];
>         $this->set('count',$count);
>         $name=$this->Session->read('User');
>         $query1=mysql_query("select id from users where username='$name'");
>     $row1=mysql_fetch_array($query1);
>         $user_id=$row1[0];
>         $this->set('user_id',$user_id);
>         $this->Entry->recursive = 1;
>         $this->set('entries', $this->Entry->findAll(null, null, array
> ('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
>     }
>
>          function view($id = null){
>          if (!$id){
>          $this->Session->setFlash('Invalid id for Entry.');
>          $this->redirect('/entries/index');
>          }
>          $this->set('entry', $this->Entry->read(null, $id));
>          }
>
>     function add(){
>     $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id')));
>         if (!empty($this->data) &&
>              is_uploaded_file($this->data['Entry']['File']
> ['tmp_name']))
>                 {
>                 $fileData = fread(fopen($this->data['Entry']['File']
> ['tmp_name'], "r"),
>                                      $this->data['Entry']['File']
> ['size']);
>             $this->data['Entry']['name'] = $this->data['Entry']['File']
> ['name'];
>             $this->data['Entry']['type'] = $this->data['Entry']['File']
> ['type'];
>             $this->data['Entry']['size'] = $this->data['Entry']['File']
> ['size'];
>             $this->data['Entry']['data'] = $fileData;
>                                 if ($this->Entry->save($this->data)){
>                                 $id=mysql_insert_id();
>                                 $query=mysql_query("select max(counter) from 
> entries");
>                                 $row=mysql_fetch_array($query);
>                                 $co=$row[0]+1;
>                                 $q=mysql_query("update entries set 
> counter=$co where id=$id");
>                                 $this->Session->setFlash('The Entry has been 
> saved');
>                                 $this->redirect('/entries/index');
>                 }
>         else{
>         $this->Session->setFlash('Please correct errors below.');
>         $this->redirect('/entries/add');
>         }
>     }
>         }
>
> }?>
>
> images are storing in database but how to display it i don't know
> my view code
> <table style="background-color:#eeeeee;border:0px;width:100%;"><tr
> style="background-color:#eeeeee;">
> <tr style="background-color:#eeeeee;" ><td style="background-
> color:#eeeeee;width:100%;border-bottom: 1px solid #eeeeee;border-
> right: 1px solid #eeeeee;" ><h2>Welcome <?php echo $id=$session->read
> ('User')?></h2></td></tr>
> </table>
> <div class="contententry">
> <h1>View Entry</h1>
>  <?php
> echo $html->css('style');
> ?>
> <table cellpadding="0" cellspacing="0">
> <tr><td  style="background:#b0c1d9;">Id</td>
> <td style="background:#eeeeee;"><?php echo $entry['Entry']['id']?></
> td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Name</td>
> <td style="background:#eeeeee;"><?php
>         if(strlen($entry['Entry']['username'])>0)
>         {
>         echo $entry['Entry']['username'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Address</td>
> <td style="background:#eeeeee;"><?php if(strlen($entry['Entry']
> ['Address'])>0)
>         {
>         echo $entry['Entry']['Address'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Phone</td>
> <td style="background:#eeeeee;"><?php if(strlen($entry['Entry']
> ['Telephone'])>0)
>         {
>         echo $entry['Entry']['Telephone'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Website</td>
> <td style="background:#eeeeee;"><?php if(strlen($entry['Entry']
> ['Website'])>0)
>         {
>         echo $entry['Entry']['Website'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
>
> <tr><td  style="background:#b0c1d9;">Section</td>
> <td  style="background:#eeeeee;"><?php echo $entry['Section']
> ['section']?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Submenu</td>
> <td  style="background:#eeeeee;"><?php echo $entry['Submenu']
> ['submenu']?></td>
> </tr>
> <tr><td style="background:#b0c1d9;">Listitem</td>
> <td  style="background:#eeeeee;"><?php
>         if(strlen($entry['Entry']['Listitem'])>0)
>         {
>         echo $entry['Entry']['Listitem'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;">Detail</td>
> <td  style="background:#eeeeee;"><?php
>         if(strlen($entry['Entry']['Detail'])>0)
>         {
>         echo $entry['Entry']['Detail'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
> <tr><td  style="background:#b0c1d9;" width="30%">Attachment</td>
> <td  style="background:#eeeeee;"><?php
>         if(strlen($entry['Entry']['name'])>0)
>         {
>         echo $entry['Entry']['name'];
>         }
>         else{
>         echo "----";
>         }
>         ?></td>
> </tr>
>
> ----------------------------------------here i m displaying image
> --------------------------------------------------
> <tr><td  style="background:#b0c1d9;" width="30%">Image</td>
> <td  style="background:#eeeeee;"><?php
> echo $html->image(array('controller'=>'entries','action'=>'show',$entry
> ['Entry']['data']));
> ?></td>
> </tr>
> </table>
> <table style="border:0px; background-color:#00254A;width:100%;border-
> bottom:0px;" >
> <tr style="background-color:#00254A;" ><td style="background-
> color:#00254A;width:8%;text-align: center;border-right: 1px solid
> #fff;" >
> <?php echo $html->link('Edit entry',   '/entries/edit/' . $entry
> ['Entry']['id'],array('class'=>'ppstyle'))?></td>
> <td style="background-color:#00254A;width:8%;text-align:center;border-
> right: 1px solid #fff;"><?php echo $html->link('Delete entry', '/
> entries/delete/' . $entry['Entry']['id'], array
> ('class'=>'ppstyle',null, 'Are you sure you want to delete: id ' .
> $entry['Entry']['id'] . '?')) ?></td>
> <td style="background-color:#00254A;width:8%;text-align: center;border-
> right: 1px solid #fff;" ><?php echo $html->link('Home',   '/entries/
> index',array('class'=>'ppstyle')) ?></td>
> <td style="background-color:#00254A;width:8%;text-align:center;border-
> right: 1px solid #fff;" ><?php echo $html->link('Add new',        '/entries/
> add',array('class'=>'ppstyle')) ?></td>
> <td style="background-color:#00254A;width:50%;text-align:
> center;border-right: 1px solid #fff;"></td>
> </tr>
> </table>
> </div>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
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