php檔案上傳執行個體並插入資料庫

來源:互聯網
上載者:User


xlsx檔案上傳到工作空間,並將資料存入資料庫Mysql


controller層

Upload.php


<?php
class Upload extends CI_Controller {


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


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


    public function do_upload()
    {
        $config['upload_path']      = 'F:/Apache24/htdocs/uploads';
        $config['allowed_types']    = 'xlsx|doc|txt|docx';
        $config['max_size']     = 1000;
        $config['max_width']        = 0;  //設定為0表示無限制
        $config['max_height']       = 0;


        define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');  


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

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


            $this->load->view('upload_form', $error);
        }
        else
        {
            // data(), 該方法返回一個數組,包含上傳檔案的所有資訊
            // data('file_name')    也可以只返回其中的一項


           $uploadResult =$this->upload->data();
          echo  $uploadResult["full_path"];

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


            $this->load->view('upload_success', $data);


            echo '<br>';
            echo dirname(__FILE__);
            require_once dirname(__FILE__) . '/../../PHPExcel/Classes/PHPExcel/IOFactory.php';


            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load( $uploadResult["full_path"]);


            echo "<br>";
            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {     // 工作空間
                echo 'Worksheet - ' , $worksheet->getTitle() , EOL;


                foreach ($worksheet->getRowIterator() as $row) {                // 工作空間的 行容器
                    echo "<br>";
                    echo '    Row number -> ' , $row->getRowIndex() , EOL;

                    $cellIterator = $row->getCellIterator();            //行容器中的列容器
                    $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set

                    $arr=array();
                    foreach ($cellIterator as $cell) {              // 每個儲存格容器

                        if (!is_null($cell)) {
                            echo "<br>";
                           // echo '        Cell -> ' , $cell->getCoordinate() , '->' , $cell->getCalculatedValue() , EOL;         // 儲存格的座標  儲存格的值
                            echo "<br><br>";

                            $data=$cell->getCalculatedValue();
                            $arr[]=$data;

                        }

                    }
                    print_r($arr);
                    $this->load->model("login_model");
                    $this->login_model->to_sql($arr);
                }
            }

        }
    }
}

?>



model層

login_model.php


<?php

class login_model extends CI_Model{
   public function __construct()
   {$this->load->database();
   }

public function to_sql($data){


        $colomnArr =array("col1","col2","col3");


        if(count($data)<count($colomnArr)){
            return;
        }


        $db_array=array();
        //遍曆一行的每一列組成一個數組
        foreach ($colomnArr as $index=>$item){
            $db_array[$colomnArr[$index]] = $data[$index];
        }


    


        $this->db->insert("sql",$db_array);


        echo  $this->db->last_query();
        echo '<hr>';
        return $this->db->affected_rows();
    }


}


view層介面:

upload_form.php

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


<?php echo $error;?>

<!--上傳檔案的建議純手寫,不建議使用CI架構的form_open_multipart()-->
<form action="/upload/do_upload" enctype="multipart/form-data" method="post">
<input type="file" name="userfile" size="20" />

<br /><br />

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

</form>

</body>
</html>


upload_success.php

<html>
<head>
 
</head>
<body>


<h3>Your file was successfully uploaded!</h3>

<ul>
    <?php foreach ($upload_data as $item => $value):?>
        <li><?php echo $item;?>: <?php echo $value;?></li>
    <?php endforeach; ?>
</ul>

<p><?php echo anchor('/upload', 'Upload Another File!'); ?></p>

</body>
</html>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.