I recently changed Web host providers and a game I wrote is now not working (it used to on my old provder's server).

When my script tries to write a file for a member who is playing, I get an "access denied" error. Here's the offending code:

$fp=fopen("$DOCUMENT_ROOT/mygame/players/$playerfile", "w");
         flock($fp, 2);    //lock file for writing
         fwrite($fp, $today);
         flock($fp, 3);   // release write lock 
         fclose($fp);    // close date file after writing member number

If a player has not played before, and the file does not exist for them, then it is created. All that the file contains is today's date (for a check to make sure they only can play once a day). If the file does not exist, the script will create it - that part works fine.
However, if the file does exist, the script is supposed to open the file for writing and overwrite with a new (today's) date. But in this case, where the file exisits and the script is trying to open it to overwrite a new date, I get an "access denied file...." error" (I have to remove the "@" to see this error). I also of course get file hanler errors on the flock, fwrite and fclose commands (since $fp was not opened).

I suspect this problem has something to do with who PhP is configured on my new server, and not seeing my script as the "owner" when it tries to open an existing file. Any ideas what might be causing this and what could be dome about it?

Thanks,
Basil

    you have to change the file permissions (chmod) so that it is writable. use your FTP client to chmod 666 or 777.

      Originally posted by emitind
      you have to change the file permissions (chmod) so that it is writable. use your FTP client to chmod 666 or 777.

      That will work for the player files that already exist. The problem is, when a new player plays and the file is written for the first time, it writes it with permissions of 644, meaning only "owner" can access it for writing later. But, the NEXT time the script tries to access the file for writing (so that it can replace the date in teh file with todays date), access is denied. Since it was working fine on my old server, I suspect that for some reason, PhP is not seen as the "owner" of the file and thus can't write. The problem seems to be associated with how PhP is installed on the new server. Obviously I can't go into FTP every time someone plays the game and CHMOD all the files.

        Ok,
        I solved the problem and will explain what I did in case anyone else ever runs into this problem when switching servers.

        As stated, I was getting "access denied" anytime my php script tried to write a file that already existed. So, I thought it might be due to the fact that the files were originally created on another server. So, I wrote a little script that read the existing files (like player1933.txt and then created an alternate file such as player1933a.txt which is writted with the contens of the original file (just a date, like 2004-03-28). Then, I deleted the original files and renamed the new files with the original file name (player1933.txt). I did this with all my player files so that now my new server is the one that created the files and when my script tries to write over the file with a new date it works and I no longer get the "access denied"

        Basil

          Write a Reply...