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.

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上