duang8642
2016-02-09 06:06
采纳率: 0%
浏览 58
已采纳

Codeigniter中的文件上传错误

I am trying to upload an image in codeigniter.

here is my view file code.

<form action="<?php echo site_url('pages/data_submitted') ?>" method="get" enctype="multipart/form-data">
Image: <input type="file" name="image"/>
<button>Submit</button>
</form>

and this is my controller code.

class Pages extends CI_Controller 
{ 
 public function data_submitted(){
 $config['upload_path']   =   "img/";
$this->load->library('upload',$config);
$this->upload->do_upload();
$finfo=$this->upload->data();
$data = $this->upload->display_errors();

$this->load->model('user_model');
$this->user_model->insert_item($data);
}
}

and here is my model code

<?php
class User_model extends CI_Model {
function __construct(){
    /* Call the Model constructor */
    parent::__construct();
}


public function insert_item($item){

    print_r($item);
}
}
?>

What is wrong with this code... Here I passed $data just to check whether any error occur or not. And it is showing "You did not select a file to upload.' even I select a file. please help me.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在codeigniter中上传图片。

这里是我的 查看文件代码。

 &lt; form action =“&lt;?php echo site_url('pages / data_submitted')?&gt;”  method =“get”enctype =“multipart / form-data”&gt; 
Image:&lt; input type =“file”name =“image”/&gt; 
&lt; button&gt;提交&lt; / button&gt; 
&lt; / form&gt  ; 
   
 
 

这是我的控制器代码。

  class Pages扩展CI_Controller 
 {
公共函数 data_submitted(){
 $ config ['upload_path'] =“img /”; 
 $ this-&gt; load-&gt; library('upload',$ config); 
 $ this-&gt; upload-&gt  ; do_upload(); 
 $ finfo = $ this-&gt; upload-&gt; data(); 
 $ data = $ this-&gt; upload-&gt; display_errors(); 
 
 $ this-&gt; 负载&GT;模型( 'user_model'); 
 $的这 - &GT; user_model-&GT; insert_item($数据); \ N} \ N} 
   
 
 

这里是我的模型代码

 &lt;?php 
class User_model extends CI_Model {
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ @  :: __ construct(); 
} 
 
 
公共函数insert_item($ item){
 
 print_r($ item); 
} 
} 
?>&gt; 
  <  / pre> 
 
 

此代码有什么问题... 我传递$ data只是为了检查是否发生错误。 它显示“您没有选择要上传的文件。” 即使我选择了一个文件。 请帮助我。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

6条回答 默认 最新

  • douxiao0400 2016-02-20 04:02
    最佳回答

    Refer this code. this will surely work for you

    public function uploadImage() {
            $this->load->helper(array('form', 'url'));  
            $config['upload_path'] = 'assets/images/b2bcategory';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '2024';
            $config['max_height'] = '1768';
            $config['width'] = 75;
            $config['height'] = 50;
            if (isset($_FILES['catimage']['name'])) {
                $filename = "-" . $_FILES['catimage']['name'];
                $config['file_name'] = substr(md5(time()), 0, 28) . $filename;
            }
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;
            $field_name = "catimage";
            $this->load->library('upload', $config);
            if ($this->input->post('selsub')) {
                if (!$this->upload->do_upload('catimage')) {
                    //no file uploaded or failed upload
                    $error = array('error' => $this->upload->display_errors());
                } else {
                    $dat = array('upload_data' => $this->upload->data());
                    $this->resize($dat['upload_data']['full_path'],           $dat['upload_data']['file_name']);
                }
                $ip = $_SERVER['REMOTE_ADDR'];
                if (empty($dat['upload_data']['file_name'])) {
                    $catimage = '';
                } else {
                    $catimage = $dat['upload_data']['file_name'];
                }
                $data = array(            
                    'ctg_image' => $catimage,
                    'ctg_dated' => time()
                );
                $this->b2bcategory_model->form_insert($data);
    
            }
        }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(5条)

相关推荐 更多相似问题