2010-05-10 73 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