??yy 2014-02-14 17:06 采纳率: 0%
浏览 41

防止覆盖文件

Have a problem with files json

http://jsfiddle.net/XLkqH/

its my code and my code php is here:

<?php
   $json = $_POST['json'];

   /* sanity check */
     $file = fopen('../js/json/nameplaylist.json','w+');
     fwrite($file, $json);
     fclose($file);

?>

I need that when the name is saved, I can not overwrite the entire file sinuses, which I added to the end of the list and then display it in a select

SOLVED:

<?php
$in_array = json_decode($_POST['json'], true);
$file_array = json_decode(file_get_contents('../js/json/nameplaylist.json'), true);
$new_array = array_merge($file_array, $in_array);
file_put_contents('../js/json/nameplaylist.json', json_encode($new_array));
?>
  • 写回答

1条回答 默认 最新

  • 7*4 2014-02-14 17:16
    关注

    I think this is what you want:

    $in_array = json_decode($_POST['json'], true);
    $file_array = json_decode(file_get_contents('../json/nameplaylist.json'), true);
    $new_array = array_merge($file_json, $in_json);
    file_put_contents('../json/nameplaylist.json', json_encode($new_array));
    

    You can't just add the new JSON to the end of the file, because then the file will contain lots of separate JSON arrays. You have to concatenate the arrays and then rewrite the whole file.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器