0

I want to write JSON data to a file using PHP. I already written { } in the file. I tried to write data in between the curly braces using fseek function. But its not working.

Thanks,

2
  • 1
    I don't think theres a need for you to write {} in your code. There is a useful function called json_encode(). Simply put an array in as a parameter, and echo it. It'll be in a JSON format. Commented May 26, 2015 at 6:32
  • For future references, you will get down voted like crazy here on SO if you do not show us that you at least tried! That is why people are asking to see your code. Commented May 26, 2015 at 6:57

1 Answer 1

2

Using Json_encode for your array instead of writing { } or any other characters:

 $array = array("Name" => "Ali" , "Lastname" =>"Gutmanz");
file_put_contents("test.txt" , json_encode($array));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your comments. But we need append data continuously whenever get data from users. I need to write data before of eof-1.
You need to add FILE_APPEND to the file_put_contents function: file_put_contents('test.txt', json_encode($array), FILE_APPEND). However, your just going to be adding the whole JSON over and over as separate strings. If your trying to open the json up and add into it's data, instead of adding over and over, then I would say your a bit over your head...
$file = fopen("datastoring.txt", "a"); $file_length = filesize($file); fseek($file, 0, SEEK_END); fwrite($file, $result); fclose($file);
You are basically doing a wrong way, why don't you store the result in the mysql database?
OK Thanks. I will try through database
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.