best first guess is that the script is trying to do something that the web server process lacks sufficient priledges to do. When you run from a command prompt, it runs as you with your priveledges. When it runs via the web server it is running as the web daemon which is usually intentionally restricted in it's priveleges. Check anywhere that you are trying to open/read/change a file to see if the web daemon has sufficient access to that(those) file(s). The ""Premature end of Script Header" simply means the code did not run... consequently, the web server did not produce a valid HTTP header.
HTH
....might should take a look at the FAQs
keep the rudder amid ship and beware the odd typo