Import Excel to mysql in php

Asked

Viewed 935 times

0

I am trying to perform an importer in php where in the input file I select the file and import the button Submit. it occurs that on this line " $data = new Spreadsheet_excel_reader("path of file");" i need to inform the complete file path 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";

1 answer

1


If the excel file is in the same php file folder, 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 "worksheets":

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

Or if you’re in a previous folder:

$caminho = dirname(__FILE__) . '/../nome_do_arquivo.xslx';
  • Thank you Diego ! I had not read about the diname(FILE), helped a lot !

  • Arrange, it is a pleasure can collaborate, abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.