PHP write to file

前端 未结 6 1913
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 14:49

below is some code I am using to \"translate\" a map array into SQL code so I can easily update my database when I have updated my game map. As you can see it prints out the

6条回答
  •  日久生厌
    2021-01-15 15:27

    If this is something you plan on writing on a regular interval or by different scripts, look at using flock() to lock the file and prevent data corruption.

        $fp = fopen("/tmp/lock.txt", "w+");
    
    if (flock($fp, LOCK_EX)) { // do an exclusive lock
        fwrite($fp, "Write something here\n");
        flock($fp, LOCK_UN); // release the lock
    } else {
        echo "Couldn't lock the file !";
    }
    
    fclose($fp);
    

提交回复
热议问题