在Codeigniter中上传文件

时间:2016-12-15 05:02:55

标签: php codeigniter

我可以保存到文件夹但无法将documedocument_foldername和document_filename保存到我的数据库中。我收到错误 -

“遇到PHP错误

严重性:注意

消息:未定义的变量:form_input_Data“

以下是我的代码

查看:

<?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> 

控制器:

    <?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); 
    }

}

请告诉我这里做错了什么。我需要你的帮助。谢谢。

3 个答案:

答案 0 :(得分:3)

请首先删除以下错误..希望它会起作用..

在视图中:在表单中使用enctype="multipart/form-data"

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

在控制器

将其删除

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

上传库的配置......

替换

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

通过

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

答案 1 :(得分:1)

请使用

  

是enctype =&#34;多部分/格式数据&#34;

在您的表单中。

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

答案 2 :(得分:0)

改变
$int = preg_replace('/[^0-9]+/', '<br />', $string);

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