PHP is not writable files find and fix


I built the following function, to critical operations with file write operations to date have, if write permissions have changed and a letter in the file is not possible. Unfortunately, the file owner will be set in most cases only by superuser, therefore sent the script a mail in case of errors.

This problem then meets with me, if my Netbeans IDE automatically upload files, I had not at all processed (Remote Synchronization), then the file is overwritten and the file owner reset.

function checkFileWritable($file)
{
    if(!file_exists($file))
    {
        file_put_contents($file, "");
    }
    if(!chmod($file, 0644))
    {
        $fileowner = fileowner($file);
        echo "counld not change File Permissions: $file, File Owner is: $fileowner";
        if(!chown($file, ftpUserID))
        {
            echo "counld not change File owner: $file ";            
            mail(mailadresse, "Schreibeprobleme","File: $file, File Owner is: $fileowner");
        }
    }
}