2012-08-03 159 views
0

我正在一個畫廊上工作,如果文件夾中沒有圖像,應該說「請上傳圖像」,但旁邊也顯示上傳的圖像,但這些東西都不起作用正確。CodeIgniter和顯示圖像/上傳圖像

沒有錯誤顯示。

上傳圖片,並改變他們進入縮略圖工作,他們在正確的文件夾中去,但是拉出來是一個不同的故事,我已經檢查了這些價值觀和他們去到正確的目的地:

$this->gallery_path = realpath(APPPATH . '../images'); 
    $this->gallery_path_url = base_url() . 'images/'; 

和這是我的自動加載:

$autoload['helper'] = array('url', 'form', 'file'); 

這就是我懷疑$數據視圖是走出來的空?:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 

<title>Gallery</title> 

</head> 

<body> 
<div id="gallery"> 
    <?php if (isset($data) && count($data)) : ;?> 


     <?php foreach($images as $image): ?> 
      <div class="thumb"> 
       <a href="<?php echo $image['url']; ?>"> 
        <img src="<?php echo $image['thumb_url']; ?>" /> 
       </div> 
     <?php endforeach; else: ?> 

     <div id="blank_gallery">Please upload an image</div> 
    <?php endif; ?> 

</div> 

<div id="upload"> 

<?php 

echo form_open_multipart('gallery'); 
echo form_upload('userfile'); 
echo form_submit('upload', 'Upload'); 
echo form_close(); 
?> 

</div> 

</body> 
</html> 

這是我的控制器:

<?php 
class Gallery extends CI_Controller 
{ 
function index() 
{ 
    $this->load->model('Gallery_model'); 
    if($this->input->post('upload')) 
    { 
     //handle upload 
     $this->Gallery_model->do_upload(); 
    } 

    $data['images'] = $this->Gallery_model->get_images(); 

    $this->load->view('gallery', $data); 
} 


} 


?> 

這是模型:

<?php 
class Gallery_model extends CI_Model{ 

var $gallery_path; 
var $gallery_path_url; 

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

    $this->gallery_path = realpath(APPPATH . '../images'); 
    $this->gallery_path_url = base_url() . 'images/'; 
} 

function do_upload() 
{ 

    //handle userfile 
    $config = array(
     'allowed_types' => 'jpg|jpeg|gif|png', 
     'upload_path' => $this->gallery_path, 
     'max_size' => 2000 
    ); 


    $this->load->library('upload', $config); 
    if(!$this->upload->do_upload()) 
    { 
     die($this->upload->display_errors()); 
    } 
    $image_data = $this->upload->data(); 


    $config = array(
     'source_image' => $image_data['full_path'], 
     'new_image' => $this->gallery_path . '/thumbs', 
     'maintain_ratio' => true, 
     'width' => 150, 
     'height' => 100 
    ); 

    $this->load->library('image_lib', $config); 
    $this->image_lib->resize(); 
     if(!$this->image_lib->resize()) 
     { 
      die($this->image_lib->display_errors()); 
     } 
} 

function get_images(){ 
    $files = scandir($this->gallery_path); 
    $files = array_diff($files, array('.', '..', 'thumbs')); 

    $images = array(); 

    foreach ($files as $file){ 
     $images[] = array(
      'url' => $this->gallery_path_url . $file, 
      'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file, 
     ); 

    } 

} 

} 

?> 

任何幫助,非常感謝!

回答

0
function get_images(){ 
    /* all the stuff you already have... */ 

    /* return the array! */ 
    return $images; 
} 
+0

您是否告訴他將模型中的所有內容移動到函數get_images中?在控制器中擁有這些功能不是更好嗎? – 2012-08-10 13:20:23

+0

我不評論他的代碼質量。我指出,get_images()函數需要返回一些東西。 – Mudshark 2012-08-14 10:29:41