【问题标题】:How to upload php or html file in codeigniter?如何在codeigniter中上传php或html文件?
【发布时间】:2017-01-01 07:25:55
【问题描述】:

如何使用上传库在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 时一切正常

【问题讨论】:

  • 您是否尝试将 allowed_types 设置为数组?它是管道字符串的替代品,不应该有所作为,但上传库可能存在问题。另外,它适用于哪个版本的 CodeIgniter?
  • 我现在尝试设置为数组,它仅适用于 jpg,不适用于 php、html、txt ...文件。 CI_version 为 3.0.6。我没有提到 do_upload 没有抛出错误消息也没有上传数据

标签: codeigniter


【解决方案1】:

首先你设置控制器,你可以使用访问权限来上传文件所有类使用
$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>

【讨论】:

    猜你喜欢
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2023-02-06
    • 2017-08-04
    相关资源
    最近更新 更多