简体   繁体   English

Codeigniter中的图像上传给出错误

[英]image upload in codeigniter giving error

I am upload an image in one my form but its always giving me an error like 我正在以我的形式上传一张图片,但它总是给我一个错误,例如

"A PHP Error was encountered Severity: Notice Message: Undefined index: vimage Filename: controllers/vouchers.php Line Number: 42" “遇到PHP错误,严重性:通知消息:未定义的索引:vimage文件名:controllers / vouchers.php行号:42”

My View File code : 我的查看文件代码:

 <?php echo form_open_multipart(site_url("promotions/add_item/"), array("class" => "form-horizontal","id"=>"addItem")) ?> <div class="form-group"> <label class="col-sm-3 control-label">Show on</label> <div class="col-sm-3"> Artist Directory : <input type="checkbox" name="artist_dir" value='1'> </div> <div class="col-sm-3"> Highlight : <input type="checkbox" name="highlight" value='1'> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">Title</label> <div class="col-sm-9"> <input type="text" class="form-control" name="title" id="title" placeholder="Title"> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">Image</label> <div class="col-sm-9"> <input type="file" name="pro_image"> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">Description</label> <div class="col-sm-9"> <textarea class="form-control" rows="3" name="description" placeholder="Description"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button> </div> </div> <?php echo form_close() ?> 

My Controller Code : 我的控制器代码:

 <pre> public function add_item(){ $this->load->library('upload'); $this->form_validation->set_rules('title','Title','required'); $this->form_validation->set_rules('description','Description','required'); print_r($_FILES['pro_image']); if ($_FILES['pro_image']['size'] > 0) { $this->upload->initialize(array( "upload_path" => './uploads/', "overwrite" => FALSE, "encrypt_name" => TRUE, "remove_spaces" => TRUE, "allowed_types" => "gif|jpg|png|jpeg", )); if (!$this->upload->do_upload('vimage')) { $this->upload->display_errors(); } $data = $this->upload->data(); echo 'img : '.$img = $data['file_name']; } exit; if($this->form_validation->run()==FALSE){ echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'. validation_errors().'</small></div>'; } else { $title = $this->input->post('title'); $description = $this->input->post('description'); $artist_dir = $this->input->post('artist_dir'); $highlight = $this->input->post('highlight'); $this->promotions_model->add_item($title, $description,$artist_dir,$highlight); } } </pre> 

please let me know what i am doing wrong here 请让我知道我在做什么错

Try this 尝试这个

  if ($_FILES['vimage']['size'] > 0) {
    $this->load->library('upload');
     $this->upload->initialize($this->set_upload_options());
                    $this->upload->do_upload();
                    $fileName = $_FILES['vimage']['name'];

    private function set_upload_options()
      { 
      // upload an image options
             $config = array();
             $config['upload_path'] = './upload/'; //give the path to upload the image in folder
             $config['allowed_types'] = 'gif|jpg|png';
              $config['max_size'] = '0';
             $config['overwrite'] = FALSE;
      return $config;
      }

For more read this Link 有关更多信息,请阅读此链接

Try this 尝试这个

if (!empty($_FILES['vimage']))
{
    $config['upload_path'] = './uploads/';
    //$config['max_size'] = '102400';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['overwrite'] = FALSE;
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = TRUE;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if (!$this->upload->do_upload('vimage'))
    {
        $this->session->set_flashdata('error', $this->upload->display_errors());
        //redirect('controller/method');
    }
    else
    {
        $this->session->set_flashdata('success', 'Image Has been Uploaded');
        //redirect('controller/method');
        $img = $data['file_name'];
    }

}

Please check first, 请先检查

print_r(_FILES); print_r(_FILES);

If you get files array then you can use below code for uploading image. 如果您得到文件数组,则可以使用以下代码上传图像。

function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

// You can give Image formats if you want to upload any video file.

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

if ( ! $this->upload->do_upload())
{
$error = array('error' = $this->upload->display_errors());

// uploading failed. $error will holds the errors.
}
else
{
$data = array('upload_data' => $this->upload->data());

// uploading successfull, now do your further actions
}
}

I have added your code in my system it's working for fine for me. 我已经在系统中添加了您的代码,对我来说很好。

public function add(){
        $this->load->library('upload');
        $this->load->helper('form');
        $this->load->helper('url');
         print_r($_FILES);die; 

            $this->form_validation->set_rules('title','Title','required');
            $this->form_validation->set_rules('description','Description','required');


            if ($_FILES['pro_image']['size'] > 0) {
                    $this->upload->initialize(array( 
                    "upload_path" => './uploads/',
                    "overwrite" => FALSE,
                    "encrypt_name" => TRUE,
                    "remove_spaces" => TRUE,
                    "allowed_types" => "gif|jpg|png|jpeg",
                ));


                 if (!$this->upload->do_upload('vimage')) {
                     $this->upload->display_errors();
                 }

                 $data = $this->upload->data();
                 echo 'img : '.$img = $data['file_name'];
            } 
            exit;

            if($this->form_validation->run()==FALSE){
                echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'.  validation_errors().'</small></div>';
            }
            else {
                $title = $this->input->post('title');
                $description = $this->input->post('description');
                $artist_dir = $this->input->post('artist_dir');
                $highlight = $this->input->post('highlight');
                $this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
            }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM