2016-02-26 89 views
1

我从笨想上传文件。文件未给予任何错误,甚至没有存储上传的文件夹whic是在根目录或文件的CodeIgniter。谁能帮助?请注意,我已经给了755允许我上传的文件夹。下面是我的代码CodeIgniter的文件上传不工作,不给错误

视图文件

<html> 
<head> 
<title>Upload Form</title> 
</head> 
<body> 

<?php echo $error;?> 

<?php echo form_open_multipart('UploadController');?> 

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

<br /><br /> 

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

</form> 

</body> 
</html> 

下面是我的控制器

<?php 

class UploadController extends CI_Controller { 

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

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

    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'; 

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

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

      $this->load->view('upload_form', $error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 

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

请帮助我在这个问题上。

回答

0

有一些错误在你的代码: 上以下行请变化:

1.更改 form_open_multipart('UploadController') form_open_multipart('UploadController/do_upload')

2.change $this->upload->do_upload("add_1") $this->upload->do_upload("userfile")。 3.change $this->load->view('upload_form', $error);
$this->load->view('uploadview', $error);

0

变化

form_open_multipart('UploadController') 

form_open_multipart('UploadController/do_upload') 
0

改变你的表格操作路径

form_open_multipart( 'UploadController/do_upload')

,或者你可以在一个方法使用这样的:

function index() 
{ 
    $config['upload_path']  = 'upload_path'; 
    $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config['max_size']   = 5120; 
    $config['encrypt_name']  = true; 

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

    $this->form_validation->set_rules('image', 'lang:image', 'file_size_max[5M]|file_allowed_type[image]'); 

    if($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('uploadview', array('error' => ' ')) 
    } 
    else 
    { 
     $user = array(); 

     $uploaded = $this->upload->do_upload('add_1'); 

    //delete the original file if another is uploaded 
    if($uploaded) 
    {   
     $data = array('upload_data' => $this->upload->data()); 

     $this->load->view('uploadview', $data); 
    } else { 
     $error = array('error' => $this->upload->display_errors()); 

     $this->load->view('upload_form', $error); 
    } 
    } 
} 

的d确保上传路径是正确的。