You are here:  » Parsing a large file with safe mode enabled

Support Forum



Parsing a large file with safe mode enabled

Submitted by ksaynor on Thu, 2007-11-08 03:46 in

Hi,

Have read through pretty much every post on this forum, but I've yet to come across a solution to my problem:

1. I have a largeish file to parse (56mb)
2. My host will not disable safe mode
3. The maximum script execution time is 30 seconds
4. Naturally I get a fatal error after 30 seconds and approx 20% of my records inserted into my DB

So here's my question:
Have you (or anyone else for that matter!) come across a method of overcoming safe mode restrictions?

Any ideas greatly appreciated.
Karl

Submitted by support on Thu, 2007-11-08 09:24

Hello Karl,

I'm afraid that due to the nature of XML, there is very little you can do as it needs to be read continuously.

One thing I can suggest however, is if you are able to execute PHP from the command line after logging in to your account via Telnet or (more likely) SSH. The most common tool people use to SSH in is called Putty, which you can get from here:

http://www.chiark.greenend.org.uk/~sgtatham/putty/

If you're not sure if you have SSH access, it might be worth trying it - use the same hostname / username / password as you do for FTP access. If you are able to login, you can then use UNIX commands to change directory to your script and try running it from the command line, for example:

$cd public_html/
$php myscript.php

(where $ is the command prompt, not part of what you type in)

It's a long-shot, and even if you can login PHP may still be time-limited, but it's worth trying...

Cheers,
David.

Submitted by ksaynor on Thu, 2007-11-08 11:37

Thanks for your really speedy reply. I will give that a shot and see if I can do anything.

I was playing around with a script called noTimeOut (http://www.phpclasses.org/browse/package/3439.html) but so far have been unable to get it to work. (It uses Ajax to asychronsously read files so that the timeout value is never hit.

But so far no luck!
Cheers,
Karl

Submitted by ksaynor on Sat, 2007-11-24 00:27

Just an update on this. Wasn't able to get either methods mentioned above working, but devised another way around the problem. Basically did the following:

1. Programatically split the large XML file (56mb) into smaller files - whilst tracking the amount of time used by php

2. Once a certain time threshold reached (or number of records) call magic parser

3. Send a header(Location:) to cause the parse page to reload (thus resetting the timeout) passing a flag indicating how far along in the large file I got

4. fseek on the large file and continue

5. Keep cycling until the EOF reached

Not particularly elegant, but reliably gets me around the 30 second timeout constraint of safe mode. Take approx 7 minutes to insert 150,000 records.