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 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流