Confused about redirecting output from awk.

I have a simple script written in awk whose purpose is to go through some php files and replace some strings. Naturally, I want the changes to be written out to the files. The script looks generally as follows:

{ 
gsub(/'replacethis'/, "with this");  # a bunch of these

print $0 > FILENAME
}

This does the replacements just fine, and writes them out to the original file, as desired. However, for files over 4096 bytes, the file is truncated to 4096 bytes. Additionally, if I change the 'print $0 > FILENAME' line to just 'print $0', it outputs the entire file into the terminal. The problem only appears to exist when writing into a file. Why is this, and what can I do about it?

Oh also, I forgot to mention that I need to do the output redirection inside of the awk script, because I run it as follows, as I want it run on every .php file.

find . -name "*.php" | xargs awk  -f fixdb.awk

You need to use temporary files, otherwise you're going to hit OS limits:

find . -name "*.php" | xargs -i awk '{f=FILENAME; gsub(/replacethis/, "with this"); print > f".tmp"} END{printf ("mv  %s %s\n", f".tmp", f ) | "sh"}' '{}' 

Insert as many gsub functions in the awk statement as needed.

Hey, thanks! I haven't gotten a chance to try it yet, but I assume it works. Could you please explain why it works, and mine doesn't? I would think that as the program parses every line, it would write out the line to the new file, so as long as the line isn't more than 4096 bytes long.

EDIT: Okay, I see that your command is different in that it calls the awk script once for each file, rather than all of the files at once, and is thus able to use a relevant END block to rename the .tmp files to the original file. But why I can I not replace the text in one go, as I was trying to do?

Actually, scratch my last reply. I'm using basically the command suggested to me above, but I'm still having the same issue. The files are still getting cut off.