2012-12-31 29 views
2

我是zend framewrok的新手。我試圖從索引視圖中顯示圖像。以下是我的代碼:如何從索引視圖顯示zend framewrok上的圖像?

<?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> 

它只顯示替代文字而不是圖像。請幫幫我。

回答

1

如果使用默認的文件夾結構試試這個:

創建一個文件夾images。把你的圖片project.png放在你的public文件夾(public/images/project.png)中。

您認爲腳本中使用的baseUrl視圖助手:

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

我用$ fileName = $ upload-> getFileName('photo'); $ fileName = basename($ fileName);獲取文件名稱可以說,我怎麼才能收到它之前重命名文件的名稱。 – user1939096

+0

檢查Zend過濾器重命名http://framework.zend.com/manual/1.12/en/zend.file.transfer.filters.html#zend.file.transfer.filters.rename – opHASnoNAME

1

如果您要訪問的圖像在PHP代碼文件夾的內容不是使用這個

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

,如果你要訪問的圖像您javascript比寫在下面的代碼在你的layout.phtml

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

now你可以像這樣訪問圖像文件夾的內容

<img src="'+config.baseUrl+'/images/project.png" /> 
+0

你能告訴我如何獲得只上傳文件的文件名。現在我使用getFileName(),它返回整個路徑 – user1939096

+0

getFileName($ file = null,$ path = true): –

+0

$ upload = new Zend_File_Transfer_Adapter_Http(); $ fileName = $ upload-> getFileName('file'); 你可以調整mycode來獲取文件名 – user1939096