I've been having the same issue. This can go wrong in two places
* max_execution_time
* memory_limit
For the sake of one script increasing both from php.ini is not recommended. So I added following in the script
---------------------------------------
set_time_limit(###);
raise_memory_limit(###_###);
----------------------------------------
This works well for me while testing and did not found any side effects yet.