php can only affect files for which it has permission so calling chmod from php is not always effective. it is recommended that you adjust all the files you need to in advance from the shell....
chmod 777 /the/file
of course having a 777 file is a Bad Thing security wise. you should reconsider how you're building your php scripts before you go around leaving files wide open like that. perhaps you need to adjust the owner of the file instead and maintain tighter permissions?