防止覆盖文件

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));
?>

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.