The file is emptied when I use file_put_contents

The main issue was resolved in the comments, although one of the bonus questions is still open and the other solution may use some improvement

It all happens in the web hosting service, the folder structure looks like this:

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

In my JS code, I am 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 am doing validation at any time to see if I am actually processing non-empty data - I log the value of the key in question, the AJAX request is only sent if it has changed, I log the data being sent and log the data. which my PHP file receives and decrypts.

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

  • params is my JSON object as one string;
  • data: my JSON object as one line with line breaks before each new object and all Cyrillic characters converted to \ u format, "did not write anything!", "PHP write success";
  • "AJAX Success"

If you then check the strogi.json file, it will be completely empty.

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

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

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

, and test.txt

as the file name.

I tried using a combination of FILE_APPEND locks | LOCK_EX and no change happens to any of the files.

My questions:

  • Why?
  • Is it possible to use a different solution if it all happens in the .done () callback for $ .getJSON () being called in the same file?

A follow-up question that deserves a separate section:

the cords are a 3D array

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

      

where the outer array contains up to two arrays. The first array contains the points of the outer polygon and (if present) the second array contains the points of the inner polygon, which serves as a cutout.

This code is an attempt to make an array representation of the coordinates for strogi.json to work for at least one object.

What I am trying to do in general is

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

If so, the constructor is called to draw the polygon using the map API, and when the build is complete, $ .ajax is used to send the coordinates retrieved through one of the API functions.

At the moment I'm sending the entire JSON object because I'm only working with one of the internal objects, but I'm guessing that retransmitting the whole thing is overkill with multiple objects represented.

  • Is there a way to pass objdata.buildings [i] .coords with index i to PHP to change the value of the "coords" key in JSON for a specific [i] object?
  • Do I need to make any changes to how the data is processed to make my JSON valid on subsequent reads? I guess I would have to change the value of "coords" from [[[x1,y1],[x2,y2]]]

    (the path it passed now) to something like this (pastebin because there is no code formatting, even if I use 4 intermediate indentation) for it to work, not is not it? How to do it?
    Solved partially by traversing the array in JS with two loops for()

    and applying toString()

    to each coordinate, there must be a better way
+3


source to share





All Articles