Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I am building a script that takes every xls file from the /uploads folder and converts it to CSV.

But, I am having this error:
**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**

Here is the code:

$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);
        }
    }


I think the error comes from wrong variable usage, but I really can't find a way to fix this. All I want to is store the name of te file in the uploads/ folder in a variable and use it throughout my script.

Here's the program without the LOOP, it works without any errors. Maybe it helps to understand better.

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


What I have tried:

To modify the code structure but with no result.
Posted
Updated 16-May-17 1:08am
v3

Move the function implementation out of the loop.

While with PHP functions can be defined anywhere in the code, they must be defined before referenced (called) when defined inside a condition (the foreach loop in your code).

See also PHP: User-defined functions - Manual[^].
 
Share this answer
 
Comments
fiftythr 16-May-17 7:08am    
I have updated the question to give a bit more detail
Jochen Arndt 16-May-17 7:26am    
Do what I and W∴ Balboos suggested:
Move the function and require_once() out of the loop:

require_once(...);
// Some code
foreach (...)
{
    // ...
    convertXLStoCSV(...);
}

function convertXLStoCSV($infile, $outfile)
{
    // ...
}
fiftythr 16-May-17 8:42am    
thank you, your example helped me put it all together, sorry I just started
Putting
require_once('Classes/PHPExcel.php');
inside a loop is not a good idea. It belongs at the top, above the loop. After all - it will only be opened once.

And the same goes for your function declaration! The first thing you should do is move both the require once and function out of the loop and see if it works a bit better for you.

Also, since each converted file will get the same name you'll end up with, at most, once converted file: the last one you convert.
 
Share this answer
 
Comments
fiftythr 16-May-17 7:09am    
I have updated the question to give a bit more detail
W Balboos, GHB 16-May-17 7:15am    
Never mind updating the question: did you try the suggestions in my answer and the one that came 5 minutes after from jochen ?
fiftythr 16-May-17 7:16am    
Yes, same error :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900