简体   繁体   English

将JSON保存到文件通常会中断

[英]Saving JSON to file often breaks

I was trying to find anwser but maybe I'm not using the right terms for my problem so I need to ask here: 我试图找到anwser,但也许我没有使用正确的术语来解决问题,所以我需要在这里询问:

I made a webapp that loads a larger array of objects from a JSON file. 我制作了一个Webapp,可从JSON文件加载更大的对象数组。 If necessary the user will update some part of the array, delete objects or can push new data. 如有必要, 用户将更新阵列的某些部分,删除对象或可以推送新数据。

Because of this when the user decides to save the array back to JSON file with JQUERY AJAX, in my PHP that handles the data I use 'w+' tag so I delete the content and write back the whole new and modified content. 因此, 当用户决定使用JQUERY AJAX将数组保存回JSON文件时,在处理数据的PHP中,我使用“ w +”标记,因此删除了内容并写回了所有新内容和修改过的内容。

The issue is that even if the JSON file is used by a single user sometimes the writing process breaks before the whole content would be writen to that cleared file . 问题是, 即使单个用户使用了JSON文件,有时写入过程也会中断,然后才能将整个内容写入该已清除文件 This couses important data loss. 这导致重要的数据丢失。

// I do make security savings to backup files, but this means every time the saving breaks I have to copy the last backup JSON file over the original JSON file from where the webapp loads data and until I do that nobody can use my webapp. //我确实为备份文件节省了安全性,但是这意味着每次保存中断时,我都必须将最后一个备份JSON文件复制到webapp加载数据的原始JSON文件上,直到没有人可以使用我的webapp。

This is my JQUERY AJAX in a .js file: 这是我在.js文件中的JQUERY AJAX:

$.ajax
({
    url: 'json/save_json.php',
    type: 'POST',
    dataType : 'json',
    data: { pid: currentdate, data: JSON.stringify(allinfobj), userlevel: jslevel},
    complete: function(){alert("this worked out!")};

});

My save_json.php: 我的save_json.php:

<?php
$datecode = $_POST['pid'];
$myFile = "work".$datecode.".json";
$userData = $_POST['userlevel'];
if($userData != 4 && $userData != 3){
    $fh = fopen($myFile, 'w+') or die("Error while opening the file.");
    flock($fh, LOCK_EX);   // acquire an exclusive lock
    $stringData = $_POST['data'];
    fwrite($fh, $stringData);
    flock($fh, LOCK_UN);
    fclose($fh);
}
?>

I use certain variables there like $userData or $datecode. 我在那里使用某些变量,例如$ userData或$ datecode。 Those have nothing to do with the fact that fwrite breaks. 这些与fwrite中断的事实无关。 Hope it's not confusing anyone, I assure you those work well. 希望它不会让任何人感到困惑,我向您保证,它们会很好地工作。

The resulting JSON file normally is something like: 生成的JSON文件通常是这样的:

[{"name":"Apple","color":"red","quality";"awful"};{"name":"Tesla","color":"metallic","quality";"improved"};{"name":"Shoe","color":"white","quality";"fake"};{"name":"Glass","color":"undefined","quality";"good"};{"name":"Jessica","color":"private","quality";"private"}]

But sometimes my result because of a break that I don't understand is this: 但是有时候我的结果是因为我不明白的休息时间是这样的:

[{"name":"Apple","color":"red","quality";"awful"};{"name":"Tesla","color":"met

So next time the user can't load this JSON data beacuase it's missing content and it's not ever a correct array or JSON. 因此,下次用户无法加载此JSON数据是因为它缺少内容,而且它也不是正确的数组或JSON。

a pr a+ in PHP I think is not a solution because I often need to replace or delete older JSON objects in the array so I can't get away by appending to the existing data, or the new array might be shorter then the old one. a PR a+ PHP中,我认为是不是一个解决方案,因为我经常需要在阵列中替换或删除旧的JSON对象,所以我不能被附加到现有的数据脱身,或新的阵列可能会短于旧。

FYI: in my ajax call success and error tags won't do anything. 仅供参考:在我的Ajax调用中, successerror标记将无济于事。 I don't know why. 我不知道为什么 complete works though, as you can see, I use it in my code. 如您所见, complete作品可以在我的代码中使用。

I hope I made my self fairly clear and I will be very grateful for any suggestions that why writing to a JSON file breaks. 我希望我能很清楚地表达自己的观点,对于任何为什么写JSON文件会中断的建议,我将不胜感激。 Where should I look to understand and fix this. 我应该在哪里了解并解决此问题。 Thanky you! 谢谢你!

Since you only write the file, then you just need w instead of w+ . 由于只写文件,所以只需要w而不是w+

But I don't think this will sovle the issue. 但是我认为这不会解决问题。

I think your server has a limit in size of your POST or GET requests. 我认为您的服务器对POSTGET请求的大小有限制。

To test it, I suggest you to see if the size of the broken JSON string is the same of the limit you set in your PHP configuration. 为了对其进行测试,建议您查看损坏的JSON字符串的大小是否与您在PHP配置中设置的限制相同。

The maximum size of a request is set in max_input_vars of PHP.INI . 请求的最大大小在PHP.INI max_input_vars中设置。

To test if the data comes correctly to your PHP script, try the following: append (in another file) the size of the JSON that you recieve. 要测试数据是否正确发送到您的PHP脚本,请尝试以下操作:在另一个文件中附加收到的JSON的大小。 When it crashes, see if the size of the broken JSON string is the same of the lenght you written in the other file. 当它崩溃时,请查看损坏的JSON字符串的大小是否与您在另一个文件中写入的长度相同。

Also, you can send, via JS, the client-side lenght of the JSON string. 另外,您可以通过JS发送JSON字符串的客户端长度。 You can then compare the reiceved lenght with the one you compute on the string you reiceve. 然后,您可以将重新获得的长度与在重新获得的字符串上计算出的长度进行比较。

This will help in discriminating if teh problem is client or server side. 这将有助于区分问题是客户端还是服务器端。

Check that the Post data is not being truncated before save it to a file. 将发布数据保存到文件之前,请检查发布数据是否被截断。 Check post_max_size if your Json Data is growing. 如果您的Json数据正在增长,请检查post_max_size。 Try too to change w+ to c Mode when you save the file. 保存文件时,也尝试将w +更改为c模式。 w+ can truncate the file. w +可以截断文件。

http://us3.php.net/manual/en/function.fopen.php http://us3.php.net/manual/en/function.fopen.php

This link can help you too: 此链接也可以为您提供帮助:

fopen(file,w+) truncates the file before I can check if it's locked with flock() fopen(file,w +)在我可以检查文件是否已被flock()锁定之前将其截断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM