Hi,
I didn't examine the code very in-depth, but I do see that you are probably going to have some problems where you attempt to include the contents of variables into parameters and or strings.
an example would be
how about trying this instead
stat($etmain. "/" .$file1);
there are other occurrences in this code where you do similar things with your variables. try fixing these up and then post back what you get and we'll work on the next step.