Update file.php

1. Replace fopen(), fwrite() and fclose() with file_put contents to avoid race conditions when writing the file contents. Currently an exclusive lock is not acquired which could cause unexpected errors when multiple php threads try to save the same file.

2. Cache cleanup optimization. Currently a cleanup is executed on every request, which is not necessary and is more or less a performance penalty (especially when having thousands of cache files, which is the case with some extensions) . I added a condition, so the cleanup is executed randomly based on probability It's currently 30%, but you can change it (or make it a setting) if you want.