first try changing a few things.
at the top of your script put:
ini_set("max_execution_time", 0);
ini_set("memory_limit", "64M");
if you are running on iis, there is a script timeout setting in the internet service manager, i cant remember exactly where it is though.