duang8642
duang8642
采纳率0%
2016-02-09 06:06

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 douxiao0400 5年前

    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);
    
            }
        }
    
    点赞 评论 复制链接分享
  • doumaji6215 doumaji6215 5年前

    Uploading file in codeigniter is much easier. Here i am writting both code for view file and controller file.

    View File

    <html>
    <body>
    <?php if(isset($error)){echo $error;} ?>
    <?php if(isset($success)){echo $success;} ?>    
    <?php echo form_open_multipart('upload_controller/do_upload');?>
    <input type='file' name='userfile' size='20' />
    <input type='submit' name='submit' value='upload' /> 
    </form>
    </body>
    </html>
    

    Controller file

    For Enable file uploading we have to load one library its "upload"

    $this->load->library('upload');

    <?php 
    
    class Upload_Controller extends CI_Controller {
    
    public function __construct() {
         parent::__construct();
    }
    
    public function index(){
        $this->load->view('fileupload_view');
    }
    
    public function do_upload(){
    
    if (!is_dir('/upload')) {
        mkdir('./upload',777,0);         
    }
    
    $config = array(
    'upload_path' => "./uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf",
    'overwrite' => TRUE,    
    );
    
    $this->load->library('upload', $config);
    
    if($this->upload->do_upload())
    {
         $data['upload_data'] = $this->upload->data();
         $data['success']= 'File Successfully Uploaded';
         $this->load->view('fileupload_view',$data);
    }
    else
    {
         $error = array('error' => $this->upload->display_errors());
         $this->load->view('fileupload_view', $error);
    }
    }
    }
    ?>
    
    点赞 评论 复制链接分享
  • doulangbi6869 doulangbi6869 5年前

    Use $this->upload->do_upload('image'); instead of $this->upload->do_upload();

    You have to pass your file input name as parameter in do_upload() . If you are not passing the field name then by default it will take userfile . Thats why it is giving You did not select a file to upload. error

    点赞 评论 复制链接分享
  • dongpei3245 dongpei3245 5年前

    try this:

    view page

    <?php echo echo form_open_multipart(base_url('Pages/data_submitted'),['name' => 'form', 'id' => 'form']);
    
      //or <form action="<?php echo base_url('Pages/data_submitted') ?>" method="post" enctype="multipart/form-data">
    
       Image: <input type="file" name="image"/>
       <button id="button" name="button">Submit</button>
    
    </form>
    

    controller

    class Pages extends CI_Controller 
    { 
        public function data_submitted(){
    
            $config['upload_path'] = getcwd().'/img/';
            $config['allowed_types'] = 'jpg|png';
            $config['max_size'] = 2500;
            $config['remove_space'] = TRUE;
    
            $this->load->library('upload',$config);
            $this->load->model('user_model');
    
            if($this->upload->do_upload(image))
            {
                $data = $this->upload->data();
                $this->user_model->insert_item($data);
            }
            else
            {
               $this->upload->display_errors();            
            }
         }
    }
    
    点赞 评论 复制链接分享
  • douli8040 douli8040 5年前

    You have to mention the field name in the do_upload method.

    <form method="post" enctype="multipart/form-data">
        <input type="file" name="field_name"/>
    </form>
    
    <?php
     $this->upload->do_upload('field_name');
     ?>
    
    点赞 评论 复制链接分享
  • doucong1853 doucong1853 5年前

    Change this:

    <button>Submit</button>
    

    to this :

    <input type="submit" value="Submit" name="submit">
    

    In Controller

    class Pages extends CI_Controller 
    { 
        public function data_submitted(){
    
            $this->load->helper(array('form', 'url'));
    
            $config['upload_path']   =   "./img/";
            $config['allowed_types'] = 'gif|jpg|png'; # Changed 
    
            $this->load->library('upload',$config);
            if(!$this->upload->do_upload())
            {
                $data = $this->upload->display_errors();
            }
            else{
                $finfo = $this->upload->data();
                $this->load->model('user_model');
                $this->user_model->insert_item($finfo);
            }
    
            # Load the view on here
    
        }
    }
    
    点赞 评论 复制链接分享

相关推荐