如何在codeigniter中上传php或html文件?

时间:2016-08-25 01:51:59

标签: codeigniter

如何使用上传库在codeigniter上传php或html文件?我有控制器在上传jpg,gif,png文件时工作正常,但它不会上传带有php或html或sql扩展名的文件。

这是我的控制器

public function ipload()
{
 $this->load->helper('url');
 $this->load->model('m_company');

 $config['upload_path']          = './uploads/';
 $config['allowed_types']        = 'php|html|txt';
 $config['max_size']             = 2048;
 $config['max_width']             = 0;
 $config['max_height']             = 0;

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

 if (!$this->upload->do_upload('userfile'))
 {
   $data = array('error' => $this->upload->display_errors());
 }
 else
 {
   $this->upload->do_upload('userfile');
   $data = array('upload_data' => $this->upload->data());
   $filen = $this->upload->data('file_name');
   $this->m_company->set_doc($filen);
 }
echo json_encode($data);
 }

当允许的文件是jpg gif png时一切正常

1 个答案:

答案 0 :(得分:0)

首先设置控制器,然后可以使用
$ config ['allowed_types'] ='*';

使用访问权限上传所有类的文件。

class c_cutipegawai extends CI_Controller{
function __construct(){
    parent::__construct();
}
function do_upload(){
    $config['upload_path']      ='./upload/SuratCuti/';
    $config['allowed_types']    ='*';
    $config['max_size']         =1024*8;
    $config['max_widht']        =1024*2;
    $config['max_height']       =768;

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

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

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

    }
}
}

在控制器中设置后,现在我们在视图中插入按钮上传文件

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


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

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

  <br /><br />

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

</form>

</body>	
</html>