3

Main issue was solved in comments, although one of the bonus questions is still open and the other's solution could use some improvement

All of this takes place on a webhosting service, the folder structure is as follows:

The JavaScript and PHP files are in /public_html/, the JSON is in /public_html/data/.

In my JS code, I'm sending a POST request with some data for my JSON file:

console.log(objdata.buildings[0].coords);
var params = JSON.stringify(objdata);
if (objdata.buildings[0].coords != " "){
    $.ajax({
        type: "POST",
        data: params,
        url: "writecoords.php",
        success: function(data){
                console.log(params);
                console.log(data);
                console.log("AJAX success");
        },
        error: function(){
                console.log("failed to send POST");
                alert("error");
        }
    });
}

PHP file:

<?php

function debug_to_console($data){
    if(is_array($data) || is_object($data))
    {
        echo("\n".json_encode($data));
    } else {
        echo("\n".$data);
    }
}

$newJSON = json_decode(file_get_contents('php://input'));
debug_to_console($newJSON);

if (is_writable('data/strogi.json')) {
    $a = file_put_contents('data/strogi.json', $newJSON);
    if(! $a)
        debug_to_console("Wrote nothing");
    debug_to_console("PHP write success");
} else {
    debug_to_console("PHP write failed");
}
?>

As you can see, I perform a check at every possible point to see if I'm actually processing non-empty data -- I log the value of the key in question, the AJAX request is sent only if it was changed, I log the data being sent and I log the data my PHP file receives and decodes.

I also check if the file is writable to avoid a possible permission problem, and only then I try to write to the file. The file comes out empty and I get the following outputs in console

  • params is my JSON object as a single line;
  • data is: my JSON object as a single line with line breaks before every new object and all cyrillic converted to \u format, "Wrote nothing!", "PHP write success";
  • "AJAX success"

If I check the strogi.json file after this, it's absolutely empty.

To rule out a problem with the format of passed JSON object, I tried writing to a simple test.txt file in the same directory as the .php, which turns out empty as well.

I tried using the method described here, but nothing changed.

I tried using a FTP upload (the method is pointed out somewhere in the comments here), and I got "No such file or directory" returned both for the strogi.json and test.txt files. I used both public_html/test.txt and test.txt as file name.

I tried using the combination of locks FILE_APPEND | LOCK_EX, and no changes happen to either of the files.

My questions are:

  1. Why?
  2. Can a different solution be used if all of this is taking place in the .done() callback for $.getJSON() called on the same file?

Follow-up question worthy of a separate section:

coords is a 3-dimensional array

[
[[x1,y1],[x2,y2],...]]
]

where the external array contains up to two arrays. The first array contains points of the external polygon and (if present) second array contains points of the internal polygon that serves as a cutout.

The code in question is an attempt to make submitting the coords array to strogi.json work for at least one object.

What I'm trying to do, in general, is

  • $.getJSON() the data/strogi.json file
  • go through the buildings[] array of objects inside it in the .done() callback
  • for each object, check if "coords" is " " (default value)

If it is, a constructor is called to build a polygon using a map API, and when construction is finished, $.ajax is used to submit coords extracted through one of API's functions.

As of now, I'm submitting the whole JSON object, because I'm only working with one of the inner objects, but I imagine resubmitting the whole thing is excessive with multiple objects presented.

  1. Is there a way to pass objdata.buildings[i].coords with the index i to PHP to change the "coords" key value in JSON for a certain buildings[i] object?
  2. Do I need to make any changes to the way I'm processing data to make my JSON valid upon further reads? I assume I'd have to change the "coords" value from [[[x1,y1],[x2,y2]]] (the way it's passed now) to something like this (pastebin because there's no code formatting even though I'm using the 4 space indent) for it to work, right? How do I do that? partly solved by going through the array in JS with two for() loops and applying toString() to every coordinate, there's gotta be a better way
bqback
  • 55
  • 9
  • 3
    You're trying to `file_put_contents` on the decoded object (probably stdClass). `file_put_contents` expects a string, array or stream as 2nd parameter. – ccKep May 29 '17 at 19:06
  • @ccKep Would "".$newJSON."" work instead? Or is there a way to use implode() on $newJSON to still write a meaningful JSON object to the file? If not, should I use fOpen, fWrite and fClose? – bqback May 29 '17 at 22:45
  • @ccKep Searched around for a bit, and the way to write objects seems to be through `serialize()` for both `fWrite()` and `file_put_contents()`, which turns the object into, well, not JSON. Is there no escaping this? Technically I could switch over to getting my JSON data through a different PHP file, which would `deserialize()` the stored mess and pass it on, but I'd very much prefer to keep it in a readable format for later direct editing. – bqback May 29 '17 at 22:57
  • 1
    If your JavaScript is posting a JSON string, simply write that string to file. There's no need to decode it if all you want to do is write some JSON – Phil May 29 '17 at 23:19
  • 1
    `file_put_contents('data/strogi.json', $newJSON);` should work just fine, if your `$newJSON` is a JSON string - no need to serialize anything. – ccKep May 30 '17 at 12:49
  • Thanks for the comments, it really was that easy! – bqback May 30 '17 at 23:32

0 Answers0