PHP File upload instance and insert Database

Source: Internet
Author: User


upload the xlsx file to the workspace and store the data in the database MySQL


Controller Layer

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; Set to 0 means no limit
$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 (), which returns an array containing all information about the uploaded file
Data (' file_name ') can also return only one of the items


$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) {//workspace
echo ' Worksheet-', $worksheet->gettitle (), EOL;


foreach ($worksheet->getrowiterator () as $row) {//Work space row container
echo "<br>";
Echo ' Row number-> ', $row->getrowindex (), EOL;

$cellIterator = $row->getcelliterator (); column containers in a row container
$cellIterator->setiterateonlyexistingcells (FALSE); Loop all cells, even if it's not set

$arr =array ();
foreach ($cellIterator as $cell) {//each cell container

if (!is_null ($cell)) {
echo "<br>";
Echo ' Cell-> ', $cell->getcoordinate (), '-> ', $cell->getcalculatedvalue (), EOL; The value of the cell's coordinate cell
echo "<br><br>";

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

}

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

}
}
}

?>



model Layer

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 ();
Iterate through each column of a row to form an array
foreach ($colomnArr as $index => $item) {
$db _array[$COLOMNARR [$index]] = $data [$index];
}





$this->db->insert ("SQL", $db _array);


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


}


View Layer Interface:

upload_form.php



<title>upload form</title>

<body>


<?php echo $error;? >

<!--suggestions for uploading files are pure handwriting and do not recommend the use of CI frame form_open_multipart ()-->
<form action= "/upload/do_upload" enctype= "Multipart/form-data" method= "POST" >
<input type= "File" Name= "UserFile" size= "

<br/><br/>

<input type= "Submit" value= "Upload"/>

</form>

</body>


upload_success.php





<body>



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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.