如何从另一个php文件调用函数?

时间:2017-07-26 10:44:43

标签: php mysql function csv multiple-files

简而言之:我上传到 / var / tmp 多个excel文件,然后将它们转换为.csv(.xls和.xlsx的2个不同转换器)。生成的文件result.csv应该插入到数据库中。这一切都奏效,直到我们决定允许同时上传多个文件(向html输入标签添加多个属性)。 问题:数据未插入表格

<?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 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;
}

我的输出: 文件已成功上传并转换为.csv

我在这里结束了;

成功插入数据库,i次(要上传的文件数)

我检查了seapartely .csv文件,它们正在正确转换。因此,错误在t2a函数中。我将不胜感激任何帮助。

3 个答案:

答案 0 :(得分:1)

在其中加入另一个文件。

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

答案 1 :(得分:1)

我认为下面这行是打开了错误的文件......

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

应该是

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

答案 2 :(得分:1)

此代码工作所需的是澄清函数的返回类型:

 function t2a ($string):string {}

解决了这个问题。