Tag Archives: maximum execution time

PHPExcel: Avoid Exceeding Maximum Execution Time CodeUnit 12 APR 2010

The CodePlex project PHPExcel is an absolutely brilliant implementation around Microsoft’s OpenXML standard, giving us a powerful PHP object driven engine that is capable of creating and reading from Excel 2007, PDF, HTML and even Excel 2003 spreadsheets and documents.

Recognise this?

Fatal error: Maximum execution time of 20 seconds exceeded in…

Today’s little hint is on how one can go about avoiding hitting the dreaded maximum execution time fata error that PHP so loves spitting out at us while generating a PHPExcel excel worksheet.

So let’s go then.

As we know, generating an excel spreadsheet using PHPExcel is remarkably simple. The following code below shows you just how simple it really is:

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=example.xls");
header('Cache-Control: max-age=0');

require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Example');
$objPHPExcel->getActiveSheet()->setCellValue('A1','Hello World');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

And that’s it, all done. However, generating a big Excel file takes a lot of time and memory and can quite often cause either Apache or PHP to simply timeout or run out of resources and spit a half-baked spreadsheet back at you.

Now to fix the memory allocation problem we can skip through by using PHP’s script cheat ini_set(‘memory_limit’, ‘-1’); which basically frees our script from the normal constraints in terms of memory usage and allocation enforced upon it.

In order to solve the maximum execution time exceeded problem we do pretty much the same thing.

Using the incredibly useful cheat of set_time_limit(20);, we basically identify the big looping structures that are involved in the generation of our spreadsheet and attach the time limit reset function inside each loop, basically now forcing the script to keep adding time to it’s execution timer and thus allowing it to essentially run forever until the loops complete their work.

In practise, you code would look like this:

foreach ($dataset as $dataitem)
{
    set_time_limit(20);
    //create the necessary worksheet cells for this particular item
}

And that is pretty much that. Implementing this approach should now free you from all those nasty, unexpected timeout fatal errors!

Force Your Script to Run Longer than Maximum Execution Time CodeUnit 08 MAR 2010

I have to process some large CSV files that generate a lot of SQL statements that need to be executed. Naturally, trying to parse any of the files almost always results in my script spitting back that horrible maximum execution time exceeded error message, even though I’ve adjusted the maximum execution time setting in the php.ini file to as large as I dare go.

So how does one go about forcing a script to stay alive infinitely until it eventually finishes its job?

(Note: You really don’t want to apply what follows to an infinite loop snippet of code!)

Well, PHP does hand us the nifty set_time_limit() function that basically restarts PHP’s built in timeout counter, setting it to zero and then changing the new timeout value to the number of seconds specified in the function call. So for example, if the timeout default is 30 seconds and you call set_time_limit(20) 25 seconds into script execution, the script will now be able to run 45 seconds before timing out.

Now calling the function with a seconds parameter of zero is said to remove the time limit altogether, though in practice you may find that this doesn’t always work exactly how it should.

If for example your long-running script is based on a long loop operation, the easiest way to ensure your script doesn’t time out is to call the set_time_limit function with a specified timeout duration of say 20 seconds for each and every loop iteration.

This will in essence keep resetting the timeout counter and extending the maximum execution time, thus resulting in a script that has a potential to run just about forever! :)

[Unless of course you are running your script under II7 on a Windows Server 2008 machine where you’ll have to adjust some additional Windows Environment parameters! Something to note though is that this function won’t work if you are running PHP in SAFE mode. Unfortunately there doesn’t seem to be a workaround for this instance! :( ]

Related Link: http://php.net/manual/en/function.set-time-limit.php