2

I'm passing a fairly large (but not huge) Json object using ajax, and writing it to a file using PHP. The $.post command works fine, but the data in the file is truncated (though still in perfect JSON formatting.

I can't figure out why.

Here's the JavaScript command. "output" is a javascript objects that contains arrays within arrays.

function saveWerJSON (output) {
        console.log(output);
        $.post("writefile.php", output, function(output){ console.log("yes"); });
};

Here's the PHP file in its entirety

<?php 
file_put_contents("wer1861.json", json_encode($_POST,  JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT));
?>

the file cuts off after 1422 lines (length: 40300). The problem is not with the encoding, I don't think, but with the length. Because if I change the order of the variables it cuts off in a different place--but at the same length.

I should add that the json in the file is valid. So it's the variable that's getting truncated. Not the file itself.

Any ideas?

Steve Fars
  • 31
  • 5
  • What server are you running? http://stackoverflow.com/questions/2880722/is-http-post-limitless – 000 Apr 08 '14 at 17:03
  • How much memory do you have allocated for php? – 000 Apr 08 '14 at 17:05
  • I'm running it on wamp on a windows 7 machine right now. Not sure how to check memory allocation. But the object is not huge. I'd be surprised if it has 5000 elements, non of them bigger than a short string or integer. – Steve Fars Apr 08 '14 at 17:10
  • Try the LOCK_EX flag, or try replacing it with the calls to fopen, fwrite, and fclose. http://us1.php.net/function.file-put-contents – 000 Apr 08 '14 at 18:28
  • tried that. It doesn't seem to work. – Steve Fars Apr 09 '14 at 02:17
  • it doesn't look like the problem is in the writing of the file, but more like the object itself is truncated during the passing from javascript to php... – Steve Fars Apr 09 '14 at 02:18
  • I think I've identified the problem. The object seems to be passed correctly, but it gets truncated in JSON_ENCODE. I'm going to repost this as a new question. – Steve Fars Apr 09 '14 at 15:50

2 Answers2

0

Having a similar issue this morning while passing a big array that was being truncated; finally solved using JSON.stringify with problematic parameter and decoding on server side. :) Now it's working. Try it.

Nuajan
  • 1
0

FWIW: I just faced the exact same problem. I have two servers I use; one is a development server available only on an intranet & which runs HTTP. The production machine runs HTTPS and is publicly available. Transmission to the development server was consistently truncating the first sub-array of my JSON array to 17 rows regardless of what data was represented in that array. Transmission to the production server works just fine.

Here is the jQuery:

var json = {
     name: name ? name : "HTML table",
     head: jsonHead,
     body: jsonBody,
     foot: jsonFoot,
     width: width,
     headRowCount: table.find("thead tr").length,
  };

  $.ajax({
     type: "POST",
     url: "/iTrain/ajax/common.php",
     data: {
        protocol: "table2excel",
        json: json,
     },
     dataType: "json",
     success: function (data) {
        if (data.file) {
           window.open(data.file, "_blank");
           console.log("output file was stored to " + data.file);
        }
     },
  });

And here is the PHP that I used to ask IIS what it heard:

echo json_encode($_REQUEST["json"]);

Examining the JSON array before it was passed showed that the json.body element correctly contained 20 rows, but examining the response showed only 17. The other elements were all received correctly.

I'm going to stop trying to wrap my brain around this one since it works in production.

Michelle
  • 57
  • 1
  • 7