Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call functions from another php file?

In short: I am uploading to /var/tmp multiple excel files, then convert them into .csv(2 different converters for .xls and .xlsx). The resulting file, result.csv should be inserted into database. It all worked until we decided to allow to upload multiple files simultaneously(adding multiple attribute to html input tag). Problem: data not inserted into 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";}
  ?>

t2a function:

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 output: File successfully uploaded and converted to .csv

cycle i ended here;

Successfully inserted into DB, i times(number of files to be uploaded)

I have checked seapartely .csv files, they are being converted correctly. Thus, the error is in t2a function. I will appreciate any help.

like image 218
amelie Avatar asked Dec 04 '25 08:12

amelie


2 Answers

Include the another file in it.

<?php include('yourfilename'); ?>
like image 191
Anand Pandey Avatar answered Dec 05 '25 23:12

Anand Pandey


I think the line below is opening the wrong file...

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

Should be

fopen("/var/tmp/result.csv", "r")
like image 39
Nigel Ren Avatar answered Dec 06 '25 00:12

Nigel Ren