我正在构建一个脚本,它从/ 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);
}
你调用该函数的被创建之前。 .. – ThisGuyHasTwoThumbs
您不需要在每个循环迭代中重新定义函数,只需为整个脚本定义一次。 –