2010-05-10 74 views
1

我對codeigniter仍然很陌生。我遇到的問題是,文件上傳正常,它寫入數據庫沒有問題,但它只是不會返回到上傳表單。相反,它保留在do_upload中,不顯示任何內容。更奇怪的是,幕後出現了一些源代碼,這些源代碼似乎是當您不選擇要上傳的圖像時出現的消息。有人可以告訴我什麼是我做錯了,因爲我想提交後返回到我的上傳表單。提前致謝。下面是我的代碼:Codeigniter在圖像上傳後沒有返回上傳表單

控制器:

function do_upload() 
{ 
    if(($error = $this->Upload_model->do_upload()) === true) 
    { 

     $this->load->view('home/upload_form'); 

    }else{ 

     $this->load->view('home/upload_success', $error); 

    } 

} 

型號:

function do_upload() 
{ 
    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '2000'; 

    $this->load->library('upload', $config); 

    if (! $this->upload->do_upload()) 
    { 
     $error = array('error' => $this->upload->display_errors()); 

     return $error; 
    } 
    else 
    { 
     $data = $this->upload->data(); 
     $full_path = 'uploads/' . $data['file_name']; 

     $spam = array(
      'image_url' => $full_path, 
      'url' => $this->input->post('url') 
     ); 

     $id = $this->input->post('id'); 

     $this->db->where('id', $id); 
     $this->db->update('NavItemData', $spam); 

     return true; 

    } 
} 

視圖(稱爲upload_form):

<html> 
<head> 
<title>Upload Form</title> 
</head> 
<body> 



<?php if(isset($buttons)) : foreach($buttons as $row) : ?> 


<h2><?php echo $row->image_url; ?></h2> 
<p><?php echo $row->url; ?></p> 
<p><?php echo $row->name; ?></p> 
<p><?php echo anchor("upload/update_nav/$row->id", 'edit'); ?></p> 


<?php endforeach; ?> 
<?php endif; ?> 

</body> 
</html> 

下面是實際的上傳表單:

<html> 
<head> 
<title>Upload Form</title> 
</head> 
<body> 



<?php echo form_open_multipart('upload/do_upload');?> 
<?php if(isset($buttons)) : foreach($buttons as $row) : ?> 
<h2><?php echo $row->name; ?></h2> 
<input type="file" name="userfile" size="20" /> 
<input type="hidden" name="oldfile" value="<?php echo $row->image_url; ?>" /> 
<input type="hidden" name="id" value="<?php echo $row->id; ?>" /> 
<br /><br /> 
<label>Url: </label><input type="text" name="url" value="<?php echo $row->url; ?>" /><br /><br /> 
<input type="submit" value="submit" /> 

</form> 

<?php endforeach; ?> 
<?php endif; ?> 

</body> 
</html> 

回答

1

您是否嘗試過重定向()函數?

redirect('upload'); 
+0

乾杯夥計,這是做的伎倆! – Drew 2010-05-10 12:59:14

0

你的源代碼中有一些非常令人困惑的部分(比如爲什麼當視圖不包含上載表單時稱爲上傳表單)。

我看到您的控制器中有一個問題,那就是條件將始終執行第一個分支。因此,您可能需要將其更改爲:

if(($error = $this->Upload_model->do_upload()) === true) 

這將捕獲錯誤,並在出錯時執行第二個分支。如果這沒有幫助,請嘗試發佈上傳表單。

+0

正如我所說,我仍然是真正新的ci和命名文件邏輯上從來沒有我的強項!他們往往有一個有機的演變。 感謝您的答案,但唉,我仍然沒有喜悅。 – Drew 2010-05-10 11:24:23