in reply to using loop/array to write files

It sounds like the console of your local machine is more accessable than the logs of your server

It might be a lot easier to debug your script if you were running it locally. ActiveState is very quick to install.

I would download a set of 20 or so ads, change the value of $adfile and  $hits_dir to something that made sense on your local machine.

Unless you have gigabytes of data to manipulate, I would even be tempted to create the files locally & then ftp them back up<P?
--mandog