2012-08-24 63 views
3

我有Codeigniter URL的問題。我有一個控制器「的welcome.php」:Codeigniter圖像和源URL

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class Welcome extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
    } 

    public function index() 
    { 
     $data['tiung'] = 'index'; 
     $this->load->view('welcome_message',$data); 
    } 

    public function dor($bus) 
    { 
     $data['tiung'] = $bus; 
     $this->load->view('welcome_message',$data); 
    } 
} 

和視圖「welcome_message.php」:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Welcome to CodeIgniter</title> 
</head> 
<body> 
    sesuatu <?php echo $tiung?> 
    <img src="fragor.jpg" width="720" height="246" alt=""/> 
    ladalah 
</body> 
</html> 

如果我想我用這個參數來訪問控制器功能「多爾」 :

localhost/hostname/index.php/welcome/dor/something 

它的工作原理,但問題是圖像未加載。我試圖把圖像文件放入webroot文件夾,'application'文件夾,甚至是'views'文件夾中。但圖像仍然無法加載。我應該在哪裏放置圖像文件?

+0

資產的文件夾或文件夾的CSS,然後使用SITE_URL funtion等SITE_URL( '資產/ fragor.jpg'); – Rooster

回答

4

最佳做法是在您的webroot(index.php文件夾)中創建一個/ images /目錄,並使用base_url()函數獲取指向圖像I.E的鏈接。

<img src="<?php echo base_url('images/fragor.jpg'); ?>" width="720" height="246" alt=""/> 

不要忘記要麼自動加載磁帶機或使用site_url()base_url()之前手動加載URL幫手。

此外,如果您使用CodeIgniter的重寫規則從URL中刪除index.php(它看起來不像您正在做的),請不要忘記將/ images /目錄從重寫中排除。

+0

問題是我使用Dreamweaver設計了網頁。使用「<?php echo base_url()?>」來改變站點意味着我必須改變我的頁面中的很多url鏈接。有沒有其他的解決方案,而無需更改網址鏈接? –

1

CI中有指示圖像的特定方式......像

echo img('images/gautam.gif'); 

把這個在您的視圖文件,你需要在你的根

0

雖然你打造「圖片」文件夾必須在自動加載配置中添加CI幫助程序'html',但這很容易。

<?php 
$image_properties = array(
    'src' => 'images/picture.jpg', 
    'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time', 
    'class' => 'post_images', 
    'width' => '200', 
    'height'=> '200', 
    'title' => 'That was quite a night', 
    'rel' => 'lightbox' 
); 
img($image_properties); 
?> 
/* <img src="site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a nigh`enter code here`t" rel="lightbox" />*/ 

OR JUST簡單:

<?php 
echo img('images/picture.jpg'); ?> 
// gives <img src="site.com/images/picture.jpg" />