2017-05-16 25 views
1

我正在构建一个脚本,它从/ uploads文件夹中获取每个xls文件并将其转换为CSV。PHP错误调用未定义的函数,如何给方法数据使用

但是,我有此错误:

Fatal error:
Uncaught Error: Call to undefined function convertXLStoCSV() in C:\xampp\htdocs\Technocripa-php\scandir.php:46 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Technocripa-php\scandir.php on line 46

下面是代码:

$dir = "uploads/*"; 
foreach(glob($dir) as $file) 
{ 
    if(!is_dir($file)) { echo basename($file)."\n";} 
    //-------------------------- 

    $nameAsString = (string)$file; 

    require_once('Classes/PHPExcel.php'); 

    $inputfilename = $nameAsString; 
    $outputfilename = 'convert.csv'; 

    //Usage: 
    convertXLStoCSV($inputfilename, $outputfilename); 

    function convertXLStoCSV($infile, $outfile) 
    { 
     $fileType = PHPExcel_IOFactory::identify($infile); 
     $objReader = PHPExcel_IOFactory::createReader($fileType); 

     $objReader->setReadDataOnly(true); 
     $objPHPExcel = $objReader->load($infile); 

     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); 
     $objWriter->save($outfile); 
    } 
} 

我认为错误来自错误的变量的用法,但我真的不能找到一种方法解决这个问题。我只想将te文件的名称存储在变量的uploads /文件夹中,并在整个脚本中使用它。

这是没有LOOP的程序,它的工作原理没有任何错误。也许这有助于更好地理解。

require_once('Classes/PHPExcel.php'); 

    $inputfilename = 'test2.xls'; 
    $outputfilename = 'convert.csv'; 

    //Usage: 
    convertXLStoCSV($inputfilename, $outputfilename); 

    function convertXLStoCSV($infile, $outfile) 
    { 
     $fileType = PHPExcel_IOFactory::identify($infile); 
     $objReader = PHPExcel_IOFactory::createReader($fileType); 

     $objReader->setReadDataOnly(true); 
     $objPHPExcel = $objReader->load($infile); 

     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); 
     $objWriter->save($outfile); 
    } 
+0

你调用该函数的被创建之前。 .. – ThisGuyHasTwoThumbs

+0

您不需要在每个循环迭代中重新定义函数,只需为整个脚本定义一次。 –

回答

2

创建的foreach之外的功能和内部这样称呼它低于之前声明函数:

function convertXLStoCSV($infile, $outfile) 
{ 
    $fileType = PHPExcel_IOFactory::identify($infile); 
    $objReader = PHPExcel_IOFactory::createReader($fileType); 

    $objReader->setReadDataOnly(true); 
    $objPHPExcel = $objReader->load($infile); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); 
    $objWriter->save($outfile); 
} 

$dir = "uploads/*"; 
foreach(glob($dir) as $file) 
{ 
    if(!is_dir($file)) { echo basename($file)."\n";} 
    //-------------------------- 

    $nameAsString = (string)$file; 

    require_once('Classes/PHPExcel.php'); 

    $inputfilename = $nameAsString; 
    $outputfilename = 'convert.csv'; 

    //Usage: 
    convertXLStoCSV($inputfilename, $outputfilename); 


} 
1
function convertXLStoCSV($infile, $outfile) 
    { 
     $fileType = PHPExcel_IOFactory::identify($infile); 
     $objReader = PHPExcel_IOFactory::createReader($fileType); 

     $objReader->setReadDataOnly(true); 
     $objPHPExcel = $objReader->load($infile); 

     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); 
     $objWriter->save($outfile); 
    } 

convertXLStoCSV($inputfilename, $outputfilename); 

使用它

+0

工作过,谢谢! – Adrianb

相关问题