2

I am new to zend framewrok. I am trying to show an image from index view. Below is my code:

<?php         
echo "Student List";
//$this->headTitle($this->title);
?>
<p><a href="<?php echo $this->url(array('controller'=>'index', 
        'action'=>'add'));?>">Add new student</a></p>
<table>
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Photo</th>
    <th>&nbsp;</th>
</tr>
<?php foreach($this->students as $student) : ?>
<tr>
    <td><?php echo $this->escape($student->name);?></td>
    <td><?php echo $this->escape($student->email);?></td>
    <td><img src='opt/lampp/htdocs/framework/zf-tutorial/project.png' alt='sdfsd'></td>
    <td>
        <a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'edit', 'id'=>$student->id));?>">Edit</a>
        <a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'delete', 'id'=>$student->id));?>">Delete</a>
    </td>
</tr>
<?php endforeach; ?>
</table>

It only show the alt text not the image. Please help me.

2 Answers2

1

If you use the default folder structure try this:

Create an Folder images. Put your image project.png inside of you public folder (public/images/project.png).

In your view Script use the baseUrl Viewhelper:

<img src="<?php echo $this->baseUrl() . '/images/project.png' ?>" alt='sdfsd'/>
Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • I used $fileName = $upload->getFileName('photo'); $fileName = basename($fileName); to get the file name can you say how can I rename the file name before receiving it. – user1939096 Jan 01 '13 at 06:00
  • Check Zend Filter Rename http://framework.zend.com/manual/1.12/en/zend.file.transfer.filters.html#zend.file.transfer.filters.rename – opHASnoNAME Jan 01 '13 at 19:04
1

If you want to access images folder contents in PHP code than use this

<img src="<?php echo $this->baseUrl()?>/images/project.png" />

and if you want to access images in your javascript than write below code in your layout.phtml

    <script type="text/javascript">
       var config = {baseUrl: '<?php echo $this->baseUrl() ?>'};
   </script>

now you can access images folder contents like this

<img src="'+config.baseUrl+'/images/project.png" />
Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
  • can you please tell me how to get the only file name of uploaded file. Now I am using getFileName() which returns whole path – user1939096 Dec 31 '12 at 07:35
  • getFileName($file = null, $path = true): – Fawad Ghafoor Dec 31 '12 at 09:31
  • $upload = new Zend_File_Transfer_Adapter_Http(); $fileName = $upload->getFileName('file'); can you adjust mycode to get the filename – user1939096 Dec 31 '12 at 10:11
  • $upload = new Zend_File_Transfer(); $upload->receive(); // Returns the file names from all files $names = $upload->getFileName(); // Returns the file names from the 'foo' form element $names = $upload->getFileName('foo'); – Fawad Ghafoor Dec 31 '12 at 10:17
  • I used $fileName = $upload->getFileName('photo'); $fileName = basename($fileName); to get the file name can you say how can I rename the file name before receiving it. – user1939096 Jan 01 '13 at 05:58
  • why you are using basename() ?? and can you please paste result of echo "
    "; print_r($fileName); echo "
    ";
    – Fawad Ghafoor Jan 01 '13 at 09:39
  • /opt/lampp/htdocs/framework/zf-tutorial/public/images/abf.png this is the output of print_r($fineName) – user1939096 Jan 02 '13 at 06:22
  • @user1939096 remove the basename() and than u can get abf.png – Fawad Ghafoor Jan 02 '13 at 15:03