douya7282 2016-12-15 05:02
浏览 49
已采纳

在Codeigniter中上传文件

I am able to save to the folder but couldn't get the documedocument_foldername and document_filename to be saved into my database. I get an error of -

"A PHP Error was encountered

Severity: Notice

Message: Undefined variable: form_input_Data"

Below are my codes

VIEW:

<?php //echo base_url('index.php/c_uploader/do_upload'); ?>
<!--?php echo $error;?-->
<!--?php echo form_open_multipart( base_url('index.php/c_uploader/do_upload') );?-->

<form id="form_input_Data", method="post" action="c_upload/do_upload">

    Select One or multiple Files
    <input multiple="multiple" name="userfile[]" size="20" type="file" />Allowed files: gif, png, png, pdf
    <input type="submit" value="upload" />
</form> 

CONTROLLER:

    <?php 

Class C_Upload extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->helper('URL','DATE','URI', 'FORM');
        $this->load->library('form_validation');
        $this->load->library('upload');
        $this->load->model('m_upload');
    }

    public function index(){
        $this->load->view('pakyaw/v_uploader');
    }

    public function do_upload(){
        $this->load->library('upload');

        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i<$cpt; $i++){
            $_FILES['userfile']['name']     = $files['userfile']['name'][$i];
            $_FILES['userfile']['type']     = $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']    = $files['userfile']['error'][$i];
            $_FILES['userfile']['size']     = $files['userfile']['size'][$i];    

            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload();

            $upload_data=$this->upload->data();
                $file_name  =   $upload_data['file_name'];
                $file_type  =   $upload_data['file_type'];
                $file_size  =   $upload_data['file_size'];

            // Output control
                $data['getfiledata_file_name'] = $file_name;
                $data['getfiledata_file_type'] = $file_type;
                $data['getfiledata_file_size'] = $file_size;
            // Insert Data for current file
                $this->m_upload->insertNotices($form_input_Data);

            //Create a view containing just the text "Uploaded successfully"
            $this->load->view('upload_success', $data);

        } // end of for loop
    }

    private function set_upload_options(){   
        //  upload an image options
        $config = array();
        $config['upload_path'] = 'att_uploads/';
        $config['allowed_types'] = 'gif|jpg|png|pdf';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;


        return $config;
    }
}

 ?>

MODEL:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_Upload extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function M_notice (){
        parent::Model();
    }

    function insertNotices($arrayOfNoticeFiles){
        $tableName  = "delwater_hradmindb.t_notices";
        $inputArray = $arrayOfNoticeFiles;

        $data = array(
            'document_foldername'=>$inputArray["document_foldername"],
            'document_filename'=>$inputArray["document_filename"]
        );

        $this->db->insert($tableName, $data); 
    }

}

Please tell me what I am doing wrong here. I need your help. Thank you.

  • 写回答

3条回答 默认 最新

  • doumiebiao6827 2016-12-15 05:24
    关注

    Please remove following errors first..Hope then it will works..

    In view: use enctype="multipart/form-data" in form.

    <form id="form_input_Data" method="post" action="c_upload/do_upload" enctype="multipart/form-data">
    

    In Controller

    remove it

    echo $_FILES['userfile']; die();
    

    And configuration of upload library...

    Replace

    $config['upload_path'] = 'att_uploads';
    

    By

    $config['upload_path'] = 'att_uploads/';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog