Codeigniter:您没有选择要上传的文件

问题描述:

我试图上传一个使用codeigniter的图像,不管是什么 我总是收到“您没有选择要上传的文件”错误。Codeigniter:您没有选择要上传的文件

这里是我的控制器:

<?php 
    class Post extends CI_Controller { 
     function __construct(){ 
      parent::__construct(); 
     } 

     function index(){ 
      $this->load->view('upload_form', array('error' => '')); 
     } 

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

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

      if (!$this->upload->do_upload('userfile')) 
      { 
       $error = array('error' => $this->upload->display_errors()); 
       echo json_encode($error); 
      } 
      else 
      { 
       $file_data = array('upload_data' => $this->upload->data()); 
       $data['img'] = base_url().'/images/'.$file_data['file_name']; 
       echo json_encode($data); 
      } 
    } 
    } 
?> 

这是我的看法:

<html lang="en"> 
    <head> 
     <meta charset="utf-8"/> 
     <meta http-equiv="X-UA-Compatible" content="IE-9"/> 
    </head> 
    <body> 

    <form action="" method="POST" enctype="multipart/form-data" > 
    Select File To Upload:<br /> 
    <input type="file" name="userfile" /> 
    <br /><br /> 
    <input type="submit" name="submit" value="Upload" class="btn btn-success" /> 
</form> 
    </body> 
</html> 

谁能帮我解决这个问题呢?我感谢所有帮助:)

我找到了解决方案

我按照本教程对其进行了修复。

http://www.formget.com/codeigniter-upload-image/

谢谢大家谁回答我的问题:)

试试这个

$image=$this->input->post('userfile'); // getting your posted image here 

     if (!$this->upload->do_upload($image)) //passing your image to upload 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      echo json_encode($error); 
     } 
     else 
     { 
      $file_data = array('upload_data' => $this->upload->data()); 
      $data['img'] = base_url().'/images/'.$file_data['file_name']; 
      echo json_encode($data); 
     } 
+3

这是一个文件不是一个帖子'$图像= $这个 - >输入 - >后( 'userfile的');' – user4419336

上工作,你要提交表单,按代码索引功能加载形式,你在上传功能代替提交同一页上