is there a reason for 0777 permissions? maybe 775 is more reasonable. php script is running on nginx or apatche user you set, usually it has at least group permissions