How to call functions from another php file?

In short: I upload several excel files to / var / tmp and then convert them to .csv (2 different converters for .xls and .xlsx). The resulting file result.csv should be inserted into the database. This all worked until we decided to allow multiple file uploads at the same time (adding multiple attributes to the html input tag). Problem: no data inserted into the table

<?php
// database connection goes here;
include 'convertt2a.php';
if (isset($_POST["submit"])) {
$updir = "/var/tmp/result.xlsx";
$n= count($_FILES['rawexcel']['name']);
for ($i=0; $i<$n; $i++)    {
$upfile = $updir.basename($_FILES['rawexcel']['name'][$i]);
$ext = pathinfo ($_FILES['rawexcel']['name'][$i], PATHINFO_EXTENSION);
if(is_uploaded_file ($_FILES ["rawexcel"]["tmp_name"][$i]))
{
 move_uploaded_file ($_FILES["rawexcel"]["tmp_name"][$i], $updir);
if ($ext == 'xlsx' ) {   exec("/usr/local/bin/cnvt   /var/tmp/result.xlsx      /var/tmp/result.csv "); } else 
 if ($ext == 'xls' ) {   exec("/usr/local/bin/xls2csv -x   /var/tmp/result.xls* -b WINDOWS-1251 -c /var/tmp/result.csv -a UTF-8"); } 
 echo "File  successfully uploaded and converted to .csv ";
  } 
else {
 echo "error uploading file ".$upfile;}

if (isset($_POST['provider'])) {
//select action to perform on case of different providers
if ($_POST['provider']=='tele2altel'){echo t2a("tele2");}
} 
 echo "cycle ".$i."ended here; </br>";
 }}
 else {echo "not isset post method";}
  ?>

      

Function

t2a:

function t2a ($string){

//opening .csv file, inserting into table in SAMPLEBANK TELE2ALTEL
$row =0;
if (($handle = fopen("/var/tmp/result.csv", "r"))!==FALSE){
while (($data = fgetcsv($handle, 1000, ","))!==FALSE) {
$row ++;
//we got data in $data[$i] array
if ($row==4) {$idb=$data[2];}
if ($row >6) {
$da=$data[0]; $imei = $data[1]; $ab=$data[2];$ty = NULL;
$du=$data[6]; $op = $data[3];$dir =$data[5];
$num= strlen($dir);
if ($num>=28) {$ty= $dir; $dir=NULL;}
if ($ab!==''){
$sql= "INSERT INTO tele2altel(Abonent,Opponent, Type, Data, Duration,     idBase, IMEI,direction)  
values ('$ab','$op','$ty','$da','$du', '$idb','$imei','$dir')";
$res = mysqli_query($conn, $sql);}
}}
fclose($handle);
} else {echo "unable to read file";}
$s = "Successfully inserted into DB";
return $s;
}

      

My conclusion: File downloaded successfully and converted to .csv

the cycle I ended here;

Successfully inserted into db, i times (number of files uploaded)

I checked the seapartely.csv files, they are converting correctly. So the error is in the t2a function. Any help would be appreciated.

+3


source to share


3 answers


The thing required for this code to work is to define the return type for the function:

 function t2a ($string):string {}

      



solved a problem.

+1


source


Include another file in it.



<?php include('yourfilename'); ?>

      

+1


source


I think the line below opens the wrong file ...

fopen("/var/tmp/result.xlsx", "r")

      

Should be

fopen("/var/tmp/result.csv", "r")

      

+1


source







All Articles