CodeIgniter框架下图片与数据入库后读取失败问题排查
Hey there! Let's break down why you're having trouble reading back your saved data and images, even though they're successfully storing in the database. Your hunch about the else statement in the controller is right on track—plus a couple of other small issues we can fix quickly.
Key Issues Identified
1. Undefined $image Variable in the View
Your Upload_success.php view tries to loop through $image, but your controller never passes this variable to the view. The model returns the inserted record, but you're not capturing that result or sending it to the view.
2. Nested Form Tags in Upload_form.php
You're using both form_open_multipart() (which generates a proper form tag) and a manual <form> tag—this creates invalid nested HTML and can break form submission.
3. Model Dependence on Controller-Loaded Library
Your model directly calls $this->upload->data(), but the Upload library is loaded in the controller. This violates MVC separation (models shouldn't rely on controller-specific libraries).
Fixed Code Snippets
Corrected Controller (upload.php)
Let's adjust the else block to capture the model's returned data and pass it to the view, plus pass the upload data to the model instead of letting it call the Upload library:
<?php class Upload extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('model','m'); $this->load->helper(array('form', 'url')); } public function index() { $this->load->view('upload_form', array('error' => ' ' )); } public function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('userfile')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { // Get upload data first $upload_data = $this->upload->data(); // Pass upload data to the model and capture the inserted record $image_data = $this->m->insertdata($upload_data); // Add both upload data and the saved record to the view data $data = array( 'upload_data' => $upload_data, 'image' => $image_data ); $this->load->view('upload_success', $data); } } }
Corrected Model (model.php)
Update the insertdata method to accept the upload data from the controller, keeping it independent:
<?php class model extends CI_Model { function __construct() { parent::__construct(); $this->load->database(); } public function insertdata($upload_data) { $data = array( 'name' => $this->input->post('title'), 'image' => $upload_data['file_name'], ); $this->db->insert('image', $data); $id= $this->db->insert_id(); // Simplify the query with get_where return $this->db->get_where('image', array('id' => $id))->result(); } }
Corrected View (Upload_form.php)
Remove the manual <form> tag and add form_close() to properly wrap the form:
<html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php echo form_open_multipart('upload/do_upload');?> <input type="text" name="title" /> <br /><br /> <input type="file" name="userfile" size="20" /> <br /><br /> <input type="submit" value="upload" name="file_name"/> <?php echo form_close(); ?> </body> </html>
Why This Works
- The controller now passes the
$imagevariable (containing your saved database record) to the view, so the loop inUpload_success.phpwill have valid data to display. - The nested form tags are gone, ensuring your form submits correctly.
- The model no longer depends on the controller's Upload library instance, following best MVC practices.
Give these changes a try—your image and data should display correctly on the success page now!
内容的提问来源于stack exchange,提问作者Ijaz Ahmad

