The Little Guy Posted August 10, 2012 Share Posted August 10, 2012 I am using ajax to automatically save data after it changes, the data is getting written to a json file with php. My page allows for 2+ people to work on a project at the same time so if 2 or more people are working on a project they are both sending save requests to the file. If they send a save request to the file at the exact same time, will there be any issues, such as member A overwriting member B's changes and vise versa? <?php $contents = file_get_contents("../projects/test.json"); $json = json_decode($contents, true); $tag = $_POST["tag"]; unset($_POST["tag"]); $save = $json["lastSaved"] = date("Y-m-d H:i:s"); $json[$tag] = $_POST; $json = json_encode($json); file_put_contents("../projects/test.json", $json); echo $save; How the page works: My page is kinda like a windows application, but the difference is when you save a windows application it saves everything at once. Mine only saves certain sections, and sections in my project are called "tags" so when you edit something you don't save all the tags, you only save the tag you changed. and that gets sent to the sever and saved to the json file. Quote Link to comment https://forums.phpfreaks.com/topic/266915-2-people-writing-to-same-file-at-once/ Share on other sites More sharing options...
xyph Posted August 10, 2012 Share Posted August 10, 2012 Yeah, allowing multiple people to read a file, modify it, and save it around the same time could result in lost data. Many systems include either versioning or user-locks to help avoid losing data. Alternately, you could store a checksum of the data when it's updated. Compare the original one to the one while updating... if it's different, a change has been made while you were editing. Quote Link to comment https://forums.phpfreaks.com/topic/266915-2-people-writing-to-same-file-at-once/#findComment-1368403 Share on other sites More sharing options...
The Little Guy Posted August 10, 2012 Author Share Posted August 10, 2012 I am thinking the easiest thing would be a file lock or something. So, I wrote this: <?php if(!is_file("../projects/test/save.lock")){ $handle = fopen("../projects/test/save.lock", "w"); fclose($handle); }else{ $timelimit = 2; $start = microtime(true); while(is_file("../projects/test/save.lock")){ usleep(500000); $end = microtime(true); if($end - $start > $timelimit){ echo "false"; exit; } } } $contents = file_get_contents("../projects/test/test.json"); $json = json_decode($contents, true); $tag = $_POST["tag"]; unset($_POST["tag"]); $save = $json["lastSaved"] = date("Y-m-d H:i:s"); $json[$tag] = $_POST; $json = json_encode($json); file_put_contents("../projects/test/test.json", $json); unlink("../projects/test/save.lock"); echo $save; Quote Link to comment https://forums.phpfreaks.com/topic/266915-2-people-writing-to-same-file-at-once/#findComment-1368413 Share on other sites More sharing options...
ManiacDan Posted August 10, 2012 Share Posted August 10, 2012 while(is_file("../projects/test/save.lock")){ usleep(500000); This race condition is known as the dining philosophers problem Quote Link to comment https://forums.phpfreaks.com/topic/266915-2-people-writing-to-same-file-at-once/#findComment-1368433 Share on other sites More sharing options...
The Little Guy Posted August 10, 2012 Author Share Posted August 10, 2012 while(is_file("../projects/test/save.lock")){ usleep(500000); This race condition is known as the dining philosophers problem I don't see a problem, there is an exit strategy in there, in case of a deadlock... Quote Link to comment https://forums.phpfreaks.com/topic/266915-2-people-writing-to-same-file-at-once/#findComment-1368467 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.