Jump to content

2+ people writing to same file at once


The Little Guy

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.