Import Excel to mysql in php

0

I'm trying to make an importer in php where in the file input I select the file and import the submit button. I need to enter the full path of the file for the import to work. How can I accomplish this?

 <?php

//ini_set("display_errors",0);
require_once 'excel_reader2.php';
require_once 'Class.BD.Mysqli.php';
//require_once './PHPExcel/IOFactory.php';




$db = new MysqliDB();
$db->conn();


$data = new Spreadsheet_Excel_Reader("path of file");

echo "Total Sheets in this xls file: ".count($data->sheets)."<br/><br/>";

$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{   
    if(count($data->sheets[$i]['cells'])>0) // checking sheet not empty
    {
        echo "Sheet $i:<br /><br />Total rows in sheet $i  ".count($data->sheets[$i]['cells'])."<br />";
        for($j=1;$j<=count($data->sheets[$i]['cells']);$j++) // loop used to get each row of the sheet
        { 
            $html.="<tr>";
            for($k=1;$k<=count($data->sheets[$i]['cells'][$j]);$k++) // This loop is created to get data in a table format.
            {
                $html.="<td>";
                $html.=$data->sheets[$i]['cells'][$j][$k];
                $html.="</td>";
            }
            $id_base     = $data->sheets[$i]['cells'][$j][1];
            $id_pesquisa = $data->sheets[$i]['cells'][$j][2];
            $pdv         = $data->sheets[$i]['cells'][$j][3];
            $razaosocial = $data->sheets[$i]['cells'][$j][4];
            $fone1       = $data->sheets[$i]['cells'][$j][5];
            $fone2       = $data->sheets[$i]['cells'][$j][6];
            $fone3       = $data->sheets[$i]['cells'][$j][7];
            $fone4       = $data->sheets[$i]['cells'][$j][8];
            $fone5       = $data->sheets[$i]['cells'][$j][9];
            $fone6       = $data->sheets[$i]['cells'][$j][10];
            $fone7       = $data->sheets[$i]['cells'][$j][11];
            $fone8       = $data->sheets[$i]['cells'][$j][12];
            $nf          = $data->sheets[$i]['cells'][$j][13];

//                        $sel = "INSERT INTO base(
//                                        'id_base',
//                                        'id_pesquisa',
//                                        'id_varejo',
//                                        'nome_varejo',
//                                        'fone1',
//                                        'fone2',
//                                        'fone3',
//                                        'fone4',
//                                        'fone5',
//                                        'fone6',
//                                        'fone7',
//                                        'fone8',
//                                        'numero_nf'
//                                        )values(
//                                        '$id_base',
//                                        '$id_pesquisa',
//                                        '$pdv',
//                                        '$razaosocial',
//                                        '$fone1',
//                                        '$fone2',
//                                        '$fone3',
//                                        '$fone4',
//                                        '$fone5',
//                                        '$fone6',
//                                        '$fone7',
//                                        '$fone8',
//                                        '$nf')";
//
//          $db->query($sel);
            $html.="</tr>";
        }
    }
      }   


$html.="</table>";
echo @$html;
echo "<br />Data Inserted in dababase";
    
asked by anonymous 16.08.2017 / 20:16

1 answer

1

If the excel file is in the same folder as the php file, you can use:

$caminho = dirname(__FILE__) . '/nome_do_arquivo.xslx';

If it is in another folder, just put the relative path inside the string. Ex: If it is in a folder called "spreadsheets":

$caminho = dirname(__FILE__) . '/planilhas/nome_do_arquivo.xslx';

Or if you are in a previous folder:

$caminho = dirname(__FILE__) . '/../nome_do_arquivo.xslx';
    
16.08.2017 / 21:43