1

I am trying to save a json string from a javascript file to a local file on the server using PHP, however, my json file is not being modified at all. Here is my Javascript:

function saveToFile(data){
  jsonString = JSON.stringify(data);
  $.ajax({
    url: 'php/save.php',
    data : jsonString,
    type: 'POST'
  });
}

Note that jsonString is a valid variable, and i can log it correctly into the console.

Here is my PHP:

<?php
  $data = $_POST['jsonString'];
  $f = fopen("../website-contents.json", "w") or die("fopen failed");
  fwrite($f, $data) or die("fwrite failed");
  fclose($f);
?>

Note that even tests trying to save "Hello World" to "test.txt" don't work, or through errors.

Finally, here is my folder structure: enter image description here

6
  • 2
    Try var_dumping your $_POST variable, might give you some more insight. The key 'jsonString' is most likely incorrect on the php side. Commented Jul 9, 2015 at 20:22
  • 1
    file_put_contents( $uploadLocation."/filename.json", $fileJsonString ); Make sure you have the correct write permissions on where you are going to save this. Commented Jul 9, 2015 at 20:22
  • Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jul 9, 2015 at 20:23
  • Have you watched the request / response in the browser's console? Commented Jul 9, 2015 at 20:23
  • @crowebird I echoed my $data and its all correct, however, its giving me the "fopen failed" error from my or die. Commented Jul 9, 2015 at 20:46

2 Answers 2

8

Here is your solution.

Js code

function saveToFile(data){
  jsonString = JSON.stringify(data);
  $.ajax({
    url: 'php/save.php',
    data : {'jsonString':jsonString},
    type: 'POST'
  });
}

php code.

$data = $_POST['jsonString'];
//set mode of file to writable.
chmod("../website-contents.json",0777);
$f = fopen("../website-contents.json", "w+") or die("fopen failed");
fwrite($f, $data);
fclose($f);
Sign up to request clarification or add additional context in comments.

3 Comments

The data is being parsed in correctly now, thank you! However, i am getting the message "fopen failed", even after setting the chmod. Is the path within fopen relative to the PHP/JS or absolute?
can you use $f = fopen("../website-contents.json", "w+"); instead of $f = fopen("../website-contents.json", "w+") or die("fopen failed"); and check the error ? So i can better understand what is happening here.
It was just a permissions error in the end, setting "sudo chmod 777 website-contents.json" solved it!
1

I agree with the comments pointing out you must have a permission problem. However, it will not work after you have corrected this problem either. You have

$data = $_POST['jsonString'];

but where do you set a key called jsonString? Use

function saveToFile(data){
  var jsonString = JSON.stringify(data);
  $.post("php/save.php", {
     jsonString: jsonString
  })
}

instead.

Comments

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.