2017-04-18 75 views
1

我试图让用户配置文件和笨新增个人资料相片的功能,我的simpy复制粘贴在文档中提供的文件上传代码,但它显示error.my视图文件是错误在上传图像文件

<?php echo form_open_multipart('upload/do_upload');?> 

<input type="file" name="userfile" size="20" /> 

<br /><br /> 

<input type="submit" value="upload" /> 

我控制器

<?php 

class Upload extends CI_Controller { 

    public function __construct() 
    { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url')); 
      $this->load->library('upload'); 
    } 



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

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

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

        echo'not happening'; 
      } 
      else 
      { 
        $data = array('upload_data' => $this->upload->data()); 

        $this->load->view('upload_success', $data); 
      } 
    } 
} 
?> 

它显示在这里的错误,我的意思是呼应“没有发生”,这意味着它不uploading.if有人能请帮助我,这将是巨大的。

+0

什么错误是它展示? –

+0

[为什么“有人能帮助我吗?”不是一个实际的问题?(http://meta.stackoverflow.com/q/284236) –

+0

是否web服务器对该文件夹的写权限,它被上传到?大概有90%的机会它不,这是相关 – gabe3886

回答

1

为什么不是你检查什么是确切的错误?我刚刚更新了代码,以跟踪codeigniter返回的确切错误。

if (! $this->upload->do_upload('userfile')) 
{ 
    $error = array('error' => $this->upload->display_errors()); 
    echo '<pre>'; 
    print_r($this->upload->display_errors()); 
} 
0

您已经加载库两次有时候我发现会造成一些问题,你已经把一个在构造函数中,一个在功能

尝试像下面,并在__constructor使用$this->upload->initialize($config);在功能和加载库。

https://www.codeigniter.com/user_guide/libraries/file_uploading.html#preferences

<?php 

class Upload extends CI_Controller { 

    public function __construct() 
    { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url')); 
      $this->load->library('upload'); 
    } 

    public function do_upload() 
    { 
      $config['upload_path'] = './images/'; 
      $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
      $config['max_size'] = 5000; 
      $config['max_width'] = 0; 
      $config['max_height'] = 0; 

      $this->upload->initialize($config); 

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

        echo'not happening'; 
      } 
      else 
      { 
        $data = array('upload_data' => $this->upload->data()); 

        $this->load->view('upload_success', $data); 
      } 
    } 
} 

注不需要关闭笨控制器和型号?>为 说,在用户指南

application 

// Where you upload images to 

images 

system 

index.php