-1

So I have a gaming server that automatically sends the amount of online players to a .php file that updates a .txt file to the current amount of players every minute.

However, when I try to write the contents of the .txt file into my website, it doesn't read the .txt file at all. The .txt just contains 1 number.

Example: players.txt contains one number, that number is 11 (for players online).

<h5> Come play with <?php echo file_get_contents("players.txt");?> other players</h5>

The outcome is "Come play with other players".

Martin
  • 19,815
  • 6
  • 53
  • 104
Jordon
  • 7
  • 2
  • The user that is running the web server may not have access to that file. Check the web servers error log. – Jason K Feb 03 '17 at 21:28
  • The file is in the same directory as both my index.php and the .php that rewrites the .txt. – Jordon Feb 03 '17 at 21:30
  • Depending on how the file is being written, the permissions on the file may still not be right even with it in the same folder. Try ls -al in the directory and see who the owner of the file is and what the effective permissions are. – tjfo Feb 03 '17 at 21:35
  • I'm not sure what you're saying. It's just a normal .txt file, I've even tried it on my local drive but it's not working. I'm not a great web developer but a .txt file shouldn't be this hard to read, especially when it's 1 number. – Jordon Feb 03 '17 at 21:40
  • check `file_exists('players.txt')` – vp_arth Feb 03 '17 at 21:44
  • A database would be a better environment to get and set this sort of data. You're probably experiencing some form of file locking or race conditions as one reader is trying to open the file to get the number while another system is writing to the file to update the number...... – Martin Feb 03 '17 at 22:52
  • Just make up the number, no one will be able to verify this value, and I doubt people really care much, just have a value you randomise every minute and output that. `:-)` – Martin Feb 03 '17 at 22:54

2 Answers2

0

It reads the file but assumes it's empty. Server would throw an error in case file not existed or it couldn't be opened because of permissions or other reasons.

Try this: 1. Echo random string ex. <?php echo '12' ?>
2. Create another file for ex. players1.txt with random number and get it's contents the same way ex. <?php echo file_get_contents('players1.txt'); ?>

blewherself
  • 451
  • 3
  • 7
0

I'm not a great web developer but a .txt file shouldn't be this hard to read, especially when it's 1 number.

This is subjective to the fact you know it's a txt file and you know it should contain a number, the issue you're encountering is probably logical rather than subjective. It could be an image file or a .dat file, if a process is writing to the file that file could be locked so other processes can not read the file, or the reader is denied access due to ownership, regardless of the file type or contents.

  • What does your PHP error log say?
  • Check the file has the correct group/owner permissions and update them as required.
  • Have you been clearing your static PHP cache to keep any retrieved values up to date?
  • Are you writing to the file with correct locking?
  • Using a Database is far, far more efficient and performant to using a filesystem to track this sort of data.
  • If you're using Unity be aware that it's local file and online security provisions are absolutely appauling, not saying this is an issue for you specifically, but just as a general proviso, limit accessability as much as possible. Always clean your data.

            $valueToPlaceIntoFile = (int)$_POST['playerCounter'];
    
Community
  • 1
  • 1
Martin
  • 19,815
  • 6
  • 53
  • 104